Java if-else-if

Summary: in this tutorial, you will learn how to use Java if-else-if statement to evaluate multiple conditions sequentially and execute different blocks of code depending on which condition is true.

Introduction to the Java if-else-if statement

The if statement allows you to evaluate one condition and execute a code block if the condition is true.

The if-else statement also evaluates one condition and executes one of two code blocks depending on whether the condition is true or not.

Both if and if-else statements evaluate only one condition.

Sometimes, you need to evaluate multiple conditions sequentially and execute a block of code if a condition is true. In this case, you can use the Java if-else-if statement.

Here’s the syntax of the Java if-else-if statement:

if(condition1) {
    // Code to execute if the condition1 is true
} else if (condition2) {
    // Code to execute if the condition2 is true
} else if (condition3) {
    // Code to execute if the condition3 is true
} else {
    // Code to execute if all conditions are false
}Code language: Java (java)

The if-else-if statement evaluates multiple conditions: condition1, condition2, condition3, … from top to bottom sequentially.

If a condition is true, the if-else-if statement executes a block of code that follows the if keyword and stops evaluating the remaining conditions. Otherwise, it evaluates the next condition from top to bottom.

If all the conditions are false, the if-else-if statement executes the block of code in the else clause.

If you don’t provide an else, the if-else-if statement will not execute any block and pass the control to the next statement in the program.

Java if-else-if statement examples

Let’s take some examples of using the if-else-if statement.

1) Simple if-else-if statement example

The following example uses the if-else-if statement to determine the grade based on a score:

public class App {
    public static void main(String[] args) {
        byte score = 72;
        char grade;

        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else if (score >= 70) {
            grade = 'C';
        } else if (score >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println(grade);
    }
}Code language: Java (java)

How it works.

First, declare a variable score and initialize its value to 72.

Second, declare a character variable grade that holds the grade based on the score.

Third, assign the grade to A, B, C, D, and F based on the value of the score.

Finally, display the grade which is C in this example.

Notice that the score is 72 which satisfies two conditions score >= 70 and score >= 60. But the if-else-if statement executes only the first block (grade = 'C') and stops evaluating the remaining conditions. Therefore, the grade is C but not D.

2) Using if-else-if statement with a complex condition

The following example uses the if-else-if statement to determine if someone is eligible to drive:

public class App {
    public static void main(String[] args) {
        int age = 25;
        boolean hasLicense = false;

        if (age < 18) {
            System.out.println("You are too young to drive.");
        } else if (age >= 18 && hasLicense) {
            System.out.println("You are eligible to drive.");
        } else {
            System.out.println("You are of legal age but do not have a license.");
        }
    }
}Code language: Java (java)

Output:

You are of legal age but do not have a license.Code language: Java (java)

How it works.

First, declare two variables age and hasLicense that determines whether someone can drive.

Second, check various conditions on the age and hasLicense and display various messages using the if-else-if statement.

Because no conditions are true, the if-else-if executes the code block in the else part.

Summary

  • Use Java if-else-if statement to evaluate multiple conditions sequentially and execute blocks of code depending on whether a condition is true.