Java Control Flow Statements: if...else and switch

In Java, there are a number of ways we can control the flow of the program. Control flow statements, change or break the flow of execution by implementing decision making statements.

The decision making statements in Java are:

  • if statement
  • if...else statement
  • switch statement

This post provides description and code examples of the Java control flow statements.

Java if Statement

The syntax of the if statement is:

if(condition) {
    //execute some code only if the condition evaluates to true
}

The if keyword is used to check if a condition is true or not. If it is true, then the specified code inside the curly braces are executed.

Example:

if(month == 'December') {
    System.out.println("Winter Season");
}

We use the usual mathematical operators to evaluate a condition:

  • Less than - a < b
  • Less than or equal to - a <= b
  • Greater than - a > b
  • Greater than or equal to - a >= b
  • Equal to - a == b
  • Not Equal to - a != b

We can either use one condition or multiple conditions, but the result should always be a boolean.

When using multiple conditions, we use the logical AND && and logical OR || operators.

Example using logical OR in if statement:

if(month == 'December' || month == 'January') {
    System.out.println("Winter Season");
}

Example using logical AND in if statement:

if(month == 'December' && day == '25') {
    System.out.println("Christmas Day!");
}

Java else Statement

If the result of the if statement evaluates to false and we want to action on the result, then we use the else statement.

The else statement is followed immediately after the closing braces of the if statement.

Example:

int temperature;

if(temperature <= 0) {
    System.out.println("Water in solid state");
} else {
    System.out.println("Water in liquid state");
}

In the above example, if temperature is 0 or less than 0, the “Water in solid state” is printed to the console. The else statement will not be executed.

If however, the temperature is greater than 0, the “Water in liquid state” is printed to the console.

Short Hand if…else Statement

We can also use short hand notation for the if...else statement which is knows as the ternary operator.

Syntax for the ternary operator is:

variable = (condition) ? expressionTrue : expressionFalse;

First, evaluate the condition in (). If the operation evaluates to true, then execute the expression between ? and :, else execute the condition after the :.

A way that helps me remember this is: (condition) ? true : false

Java else if Statement

We can use multiple if and else statements as long as a condition is not met.

The syntax for else if is:

if(condition1) {
    //execute some code only if condition1 evaluates to true
} else if(condition2) {
    //execute some code if condition2 evaluates to true
} else {
    //execute code is both conditions evaluate to false
}

Example:

int temperature;

if(temperature <= 0) {
    System.out.println("Water in solid state");
} else if(temperature >= 100){
    System.out.println("Water in gas state");
} else {
    System.out.println("Water in liquid state");
}

Java switch Statement

Another way to control the flow of the program is via a switch statement. The switch statement is used when we have a number of options and in each case we execute different code.

It acts similar to multiple if...else statements.

The switch Syntax

The syntax of the switch statement is:

switch(expression) {
    case a:
        //execute some code
        break;
    case b:
        //execute some other code
        break;
    default:
        //execute the default code
}

First an expression is evaluated. The outcome of the expression is compared against each case. if the outcome of the expression matches any of the case conditions, the associated block of code is executed.

The break keyword is used to exit the switch block. This is important because once a match is found, we don’t want to continue to evaluate other case conditions.

The default keyword is executed if no case match the value of the switch expression.

Both break and default are optional, but is recommended for good coding practice.

Example switch Statement

The code below uses the switch statement to see if the language is supported or not

String lang = "en";
switch (lang) {
    case "en":
        System.out.println("English");
        break;
    case "fr":
        System.out.println("French");
        break;
    case "de":
        System.out.println("Deutsch");
        break;
    default:
        System.out.println("Language not supported");
}

Output:

English

Read more about the Java Switch Statement

Summary

In this article, we covered the Java control flow statements which are if, else if and switch statements.