⏱️ 8 min read • Beginner Level • Lesson 23
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.
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.
The syntax contains one if block, one or more else if
blocks, and an optional else block.
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
}
if-else-if ladder.
Once a true condition is found, Java skips the remaining conditions.
The following diagram shows how Java checks conditions in an if-else-if ladder.
if statement.true, its block executes and the ladder ends.false, Java checks the next else if condition.else block executes, if it is present.
The following program compares the value of x with 10.
It checks whether x is greater than, less than, or equal to 10.
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
x is initialized with the value 10.x > 10 is checked. It is false.x < 10 is checked. It is also false.else block executes.x is equal to 10.
This program uses an if-else-if ladder to print a student's grade
based on marks.
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.
Scanner class to take input from the user.Scanner object named sc is created.marks.0 or greater than 100.marks >= 90, the program prints Grade A.marks >= 80, then marks >= 70, and so on.if-else-if ladder.Scanner object is closed using sc.close().The following example checks whether a day number represents a weekday, weekend, or invalid day.
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
| 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. |
= instead of == for comparison.if or else if condition.In an if-else-if ladder, order matters. General conditions should not be placed before specific conditions.
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");
}
}
}
marks >= 90
will never be checked because marks >= 60 becomes true first.
Always place more specific conditions before general conditions.
if-else-if ladder is used to check multiple conditions.else block is optional.🧠 Test your understanding with a quick quiz
Topic: If-else-if | Language: Java
int x = 10;
if (x > 10) {
System.out.println("Greater");
} else if (x < 10) {
System.out.println("Less");
} else {
System.out.println("Equal");
}int marks = 95;
if (marks >= 60) {
System.out.println("Passed");
} else if (marks >= 90) {
System.out.println("Excellent");
}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");
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.