Java If-Else-If Ladder - Syntax, Flowchart and Examples

⏱️ 8 min read • Beginner Level • Lesson 23

Lesson 23 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

The if-else-if ladder in Java is used when we need to check multiple related conditions one by one. Java checks each condition from top to bottom and executes the block of the first condition that becomes true.

Before learning the if-else-if ladder, you should understand Java if statement, Java if-else statement, and Java operators.


What is if-else-if Ladder in Java?

An if-else-if ladder is a chain of conditions. It is useful when there are more than two possible decisions and only one block should be executed.

  • Java evaluates conditions from top to bottom.

  • The first condition that becomes true executes its block.

  • If all conditions are false, the optional else block executes.

Syntax of if-else-if Ladder

The syntax contains one if block, one or more else if blocks, and an optional else block.

IfElseIfSyntax.java
Copy Download
if (condition1) {
    // executes if condition1 is true
} else if (condition2) {
    // executes if condition1 is false and condition2 is true
} else if (condition3) {
    // executes if previous conditions are false and condition3 is true
} else {
    // executes if none of the above conditions are true
}
Note: Only one block executes in an if-else-if ladder. Once a true condition is found, Java skips the remaining conditions.

Flowchart of if-else-if Ladder

The following diagram shows how Java checks conditions in an if-else-if ladder.

Java if-else-if ladder flowchart

How does it work?

  • Java first checks the condition inside the if statement.
  • If the first condition is true, its block executes and the ladder ends.
  • If the first condition is false, Java checks the next else if condition.
  • This continues until a true condition is found.
  • If no condition is true, the else block executes, if it is present.
  • Only one block executes in the entire ladder.

Real-World Uses of if-else-if Ladder

  • Assigning grades based on marks.
  • Calculating income tax slabs.
  • Determining discount percentages.
  • Selecting membership levels.
  • Checking age categories such as child, adult, and senior citizen.

Java if-else-if Ladder Example

The following program compares the value of x with 10. It checks whether x is greater than, less than, or equal to 10.

IfElseIfExample.java
Copy Try Download
public class IfElseIfExample {
    public static void main(String[] args) {
        int x = 10;

        if (x > 10) {
            System.out.println("x is greater than 10");
        } else if (x < 10) {
            System.out.println("x is less than 10");
        } else {
            System.out.println("x is equal to 10");
        }
    }
}

Output:

x is equal to 10

Explanation

  1. The variable x is initialized with the value 10.
  2. The first condition x > 10 is checked. It is false.
  3. The second condition x < 10 is checked. It is also false.
  4. Since none of the above conditions are true, the else block executes.
  5. The program prints x is equal to 10.

Example: Student Grade Program

This program uses an if-else-if ladder to print a student's grade based on marks.

GradeExample.java
Copy Try Download
import java.util.Scanner;

public class GradeExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter marks: ");
        int marks = sc.nextInt();

        if (marks < 0 || marks > 100) {
            System.out.println("Invalid marks! Please enter marks between 0 and 100.");
        } else if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 80) {
            System.out.println("Grade B");
        } else if (marks >= 70) {
            System.out.println("Grade C");
        } else if (marks >= 60) {
            System.out.println("Grade D");
        } else {
            System.out.println("Grade F");
        }

        sc.close();
    }
}

Output1:

Enter marks: 75
Grade C

Output2:

Enter marks: 95
Grade A

Output3:

Enter marks: 120
Invalid marks! Please enter marks between 0 and 100.

Explanation of Grade Program

  1. The program imports the Scanner class to take input from the user.
  2. A Scanner object named sc is created.
  3. The user enters marks, and the value is stored in the variable marks.
  4. The first condition checks whether marks are invalid, meaning less than 0 or greater than 100.
  5. If marks are valid, Java checks grade conditions from top to bottom.
  6. If marks >= 90, the program prints Grade A.
  7. If the first condition is false, Java checks marks >= 80, then marks >= 70, and so on.
  8. Only the first true condition executes in an if-else-if ladder.
  9. The Scanner object is closed using sc.close().

Another Example: Day Type Program

The following example checks whether a day number represents a weekday, weekend, or invalid day.

DayTypeExample.java
Copy Try Download
public class DayTypeExample {
    public static void main(String[] args) {
        int day = 6;

        if (day >= 1 && day <= 5) {
            System.out.println("Weekday");
        } else if (day == 6 || day == 7) {
            System.out.println("Weekend");
        } else {
            System.out.println("Invalid day");
        }
    }
}

Output:

Weekend

Difference Between if-else and if-else-if Ladder

if-else Statement if-else-if Ladder
Used for two possible outcomes. Used for multiple possible outcomes.
Contains one if and one else. Contains one if, multiple else if, and optional else.
Good for simple true/false decisions. Good for checking many related conditions.

Common Mistakes in if-else-if Ladder

  • Writing conditions in the wrong order.
  • Using = instead of == for comparison.
  • Forgetting that only the first true block executes.
  • Adding unnecessary or unreachable conditions.
  • Placing a semicolon after if or else if condition.

Mistake: Wrong Condition Order

In an if-else-if ladder, order matters. General conditions should not be placed before specific conditions.

WrongOrderExample.java
Copy Try Download
public class WrongOrderExample {
    public static void main(String[] args) {
        int marks = 95;

        if (marks >= 60) {
            System.out.println("Passed");
        } else if (marks >= 90) {
            System.out.println("Excellent");
        }
    }
}
Important: In the above example, marks >= 90 will never be checked because marks >= 60 becomes true first. Always place more specific conditions before general conditions.
Summary:
  • The if-else-if ladder is used to check multiple conditions.
  • Conditions are checked from top to bottom.
  • Only the first true block is executed.
  • The final else block is optional.
  • Condition order is important in an if-else-if ladder.

Interview Questions ⭐

The if-else-if ladder in Java is used to check multiple conditions one by one and execute the block of the first condition that becomes true.

Only one block executes in an if-else-if ladder. Once a true condition is found, the remaining conditions are skipped.

No, the else block is optional. It is used when you want to execute a default block if none of the conditions are true.

Condition order is important because Java checks conditions from top to bottom and executes the first true block. A general condition before a specific condition can make later conditions unreachable.

The if-else statement is used for two possible outcomes, while the if-else-if ladder is used for multiple possible conditions.

Next step: Learn Java Nested If

🚀 Continue to Java Nested If →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: If-else-if | Language: Java

Question 1 of 10
Q1. How many blocks can execute in a normal if-else-if ladder?
Q2. Which of the following is the correct syntax for else-if in Java?
Q3. What is the main purpose of an if-else-if ladder in Java?
Q4. What will be the output of this code?
int x = 10;
if (x > 10) {
    System.out.println("Greater");
} else if (x < 10) {
    System.out.println("Less");
} else {
    System.out.println("Equal");
}
Q5. Which statement is true about if-else-if ladder?
Q6. Is the final else block mandatory in an if-else-if ladder?
Q7. What will be the output of this code?
int marks = 95;
if (marks >= 60) {
    System.out.println("Passed");
} else if (marks >= 90) {
    System.out.println("Excellent");
}
Q8. Why should specific conditions usually be written before general conditions?
Q9. What will be the output of this code?
int marks = 75;
if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 80) {
    System.out.println("Grade B");
} else if (marks >= 70) {
    System.out.println("Grade C");
} else {
    System.out.println("Grade F");
}
Q10. What happens if all if and else-if conditions are false and there is no else block?

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.