A Beginner’s Guide To Java Selection Statements

In Java, selection statements are a way to control how a program works. They are used to choose an execution path based on a certain condition, as their name suggests.

Java has three ways to choose which code to run: if, if..else, and switch. Let’s look at them in more depth.

1. If Statement

This is a statement with only one choice. It’s called that because it only chooses or doesn’t choose one action (or group of actions).

Use the if statement when you want a certain statement to run only if a certain condition is true. A condition is an expression that returns a true or false value (1 or 0). Expressions that give a boolean result include relational, logical, and equality operations.

If the condition is not true, the action that was supposed to happen won’t happen.

Syntax:

if (condition)
statement

Sample code:

if (mark >90)
System.out.println(“You got grade A”);

Notice how the System.out.ln() statement is set apart from the rest of the code. It’s a good idea to include it to show how the program is put together. Most IDEs will add it for you when you move to the next line. So you don’t need to worry about leaving it out.

2. The if..else Statement

This is a statement with two choices. Its name comes from the fact that it can choose between two different things to do (or a group of actions).

When a condition is true, the if..else statement does something in the if block. If the condition evaluates to false, the action in the else block is carried out.

Syntax:

if (condition)
statement1
else
statement2

Sample code:

if (age < 18)
System.out.println(“You are a minor.”);
else
System.out.println(“You are an adult.”);

Nested if..else

It is possible to have if..else statements inside if..else statements, a scenario known as nesting.

See the example below:

if (temperatures > 6000)
System.out.println(” Object’s color likely blue”);
else
if (temperatures > 5000)
System.out.println(” Object’s color likely white”);
else
if (temperatures > 3000)
System.out.println(” Object’s color likely yellow”);
else
System.out.println(” Object’s color likely orange”);

The code above checks if an object’s temperature is within a certain range, and if it is, it shows what color it is likely to be. The code above is very wordy, and it will probably be hard to figure out how it works.

Check out the below. It gets the same job done, but it’s shorter and doesn’t need the after else. Most programmers actually like it better than the second option.

if (temperatures > 6000)
System.out.println(“Object’s color likely blue”);
else if (temperatures > 5000)
System.out.println(“Object’s color likely white”);
else if (temperatures > 3000)
System.out.println(“Object’s color likely yellow”);
else
System.out.println(“Object’s color likely orange”);

Blocks

Most of the time, the if and if..else statements only do one thing. If you want to run more than one statement with them, group them with braces.

if (condition)
// statements
else
// statements
3. Switch

This is a statement with more than one choice. It checks to see if an expression matches one of the given cases, and if it does, it does the action for that case.

Syntax:

switch(expression)
case a:
// statement
break;
case b:
// statement
break;
case n:
// statement
break;
default:
// statement

When a match is found, the break statement stops the switch statement from running. If a case has already been found, there’s no reason to waste any more time.

The expression in the switch statement must be a constant integral of type byte, short (but not long), int, or char. The String data type is another option.

Sample code:

String position= “E”;

switch (position)
case “N”:
System.out.println(“You are in the North”);
break;
case “W”:
System.out.println(“You are in the West”);
break;
case “S”:
System.out.println(“You are in the South”);
break;
case “E”:
System.out.println(“You are in the East”);
break;
default:
System.out.println(“Non-cardinal position”);

A Look at the Python if Statement

Now that you know how to use selection statements in Java, you might want to try using them in Python.

The way they work is the same, but Python is easier for beginners to learn and isn’t as wordy. Learning logic in more than one language helps you remember the main ideas. It’s never a bad idea to learn more than one type of coding.