Java if-else-if ladder with Examples

Last Updated : 16 Jan, 2026

The if-else-if ladder in Java is a decision-making construct used to evaluate multiple conditions sequentially. It allows a program to execute only one block of code from several possible options based on the first condition that evaluates to true. If none, an optional else block is executed.

  • Conditions are checked top to bottom
  • Only one block executes
  • Execution stops as soon as a true condition is found

Example:

Java
class IfElseIfExample {
    public static void main(String[] args) {

        int marks = 78;

        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}

Output
Grade B

Explanation:

  • Conditions are checked from top to bottom until a true condition is found.
  • Since marks is 78, the condition marks >= 75 is true, so Grade B is printed.
  • Once a true condition executes, the remaining conditions are skipped.

Syntax

if (condition1) {
// executes if condition1 is true
} else if (condition2) {
// executes if condition2 is true
} else if (condition3) {
// executes if condition3 is true
} else {
// executes if none of the above conditions are true
}

if_else_if_ladder
Flochart of Java if-else-if ladder

Working:

  1. The program starts by evaluating the first if condition.
  2. If the condition is true, the corresponding block executes and the ladder terminates.
  3. If the condition is false, control moves to the next else if condition.
  4. This process continues until a condition becomes true.
  5. If all conditions evaluate to false, the else block executes (if present).
  6. Control exits the if-else-if ladder.

Example 1: The following example checks the value of a variable and prints a message based on the matched condition.

Java
class GFG {

    public static void main(String[] args) {

        int i = 20;

        if (i == 10)
            System.out.println("i is 10");
        else if (i == 20)
            System.out.println("i is 20");
        else
            System.out.println("i is neither 10 nor 20");
    }
}

Output
i is 20

Dry-Running Above Example

  1. Program starts.
  2. Variable i is initialized to 20.
  3. First condition i == 10 is checked → false.
  4. Second condition i == 20 is checked → true.
  5. "i is 20" is printed.
  6. Remaining conditions are skipped.
  7. Program ends.

Example 2: Multiple Conditions with Final Else

Java
class GFG {

    public static void main(String[] args) {

        int i = 20;

        if (i == 10)
            System.out.println("i is 10");
        else if (i == 15)
            System.out.println("i is 15");
        else if (i == 20)
            System.out.println("i is 20");
        else
            System.out.println("i is not present");

        System.out.println("Outside if-else-if");
    }
}

Output
i is 20
Outside if-else-if

Explanation:

  • The value of i is compared against multiple conditions.
  • The condition i == 20 evaluates to true.
  • The corresponding block executes.
  • Control exits the ladder and continues with the remaining statements.

Advantages of Java if-else-if Ladder

  • Sequential Condition Checking: It allows multiple conditions to be evaluated in order by making it useful for handling a range of scenarios.
  • Readability: It is easy to read and understand for simple decision-making logic.
  • Fallback Mechanism: This provides an optional else block to handle cases where none of the conditions are met.
  • Versatile: This can be used for both numerical and logical comparisons.
  • Simpler Alternative: It is suitable for situations where using switch is not feasible, such as complex conditions.
Comment