A Beginner’s Guide to Java Selection Statements

Selection statements are a program control structure in Java. As the name suggests, they are used to select an execution path if a certain condition is met.

There are three selection statements in Java: if, if..else, and switch. Let’s take a closer look at them.

1. The if Statement

This is a single selection statement. It is named so because it only selects or ignores a single action (or group of actions).

When you want a certain statement to execute if a given condition is true, then use the if statement. A condition is any expression that gives a boolean result, i.e true or false (1 or 0). Relational, logical, and equality operations are such types of expressions that give a boolean result.

If the condition is false, then the execution of the supposed action will be skipped.

Syntax:

if (condition)
statement

Sample code:

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

Notice the indentation before the System.out.ln() statement. It’s good practice to include it in order to show the program structure. Most IDEs automatically include it as you move to the next line. So you shouldn’t worry about forgetting to include it.

2. The if..else Statement

This is a double selection statement. It is named so because it chooses between two different actions (or a group of actions).

Related: How to Use IF Function With Nested Formulas in Excel

The if..else statement executes a certain action in the if block when a condition is true. Otherwise, it executes an action in the else block when the condition evaluates to a false result.

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 above code checks if an object’s temperature is within a certain range and then prints its likely color. The code above is verbose and you’ll most likely find it confusing to follow through with the logic.

Look at the one below. It achieves the same goal, but it’s more compact and doesn’t have the unnecessary { } after else. Most programmers actually prefer it to the latter.

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

The if and if..else statements generally expect to execute one action. If you wish to execute multiple statements with them, use braces { } to group these actions.

if (condition){
// statements
} else {
// statements
}

3. Switch

This is a multiple-selection statement. It checks if an expression matches one of the given cases and then executes an action for that case.

Syntax:

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

The break statement is used to stop the switch statement from running when a match has been found. There’s no need to waste execution time if a case has been found.

The expression given in the switch statement must be a constant integral of type byte, short (but not long), int, or char. You can also use the String data type.

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 have learned how to use selection statements in Java, it may be interesting to shift to Python.

The programming logic is similar, but Python is more beginner-friendly and not as wordy. Learning logic in multiple languages helps enforce the underlying ideas being practiced. It’s never a bad idea to diversify your coding knowledge.

Author: Jerome Davidson

Source: Jerome Davidson.” A Beginner’s Guide to Java Selection Statements”. Retrieved From https://www.makeuseof.com/java-selection-statements/

All Rights Of This Article Reserved To MakeUseOf

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: