Java if-else-if Statement
Syntax and Examples
In Java, if-else-if
ladder is used to evaluate multiple conditions in a structured manner. If you’ve already understood simple if and if-else blocks, this is the next natural step.
The if-else-if
ladder allows you to test multiple conditions, one after another. As soon as one condition evaluates to true
, the corresponding block is executed, and the rest are skipped.
Syntax of if else if
in Java
if (condition1) {
// code block 1
} else if (condition2) {
// code block 2
} else if (condition3) {
// code block 3
} else {
// default code block
}
- The program evaluates each condition from top to bottom.
- Once it finds a condition that is
true
, it executes that block and ignores the rest. - If none of the conditions are
true
, theelse
block runs (if provided).
Example 1: Grade Classification
public class GradeExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D or lower");
}
}
}
Grade: B
Explanation:
Here, score
is 85. It first checks if it's greater than or equal to 90 (false), then checks if it's greater than or equal to 80 (true). So, it prints "Grade: B" and exits the ladder.
Example 2: Weather Message
public class WeatherExample {
public static void main(String[] args) {
int temperature = 5;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else if (temperature > 20) {
System.out.println("It's a warm day.");
} else if (temperature > 10) {
System.out.println("It's a bit chilly.");
} else {
System.out.println("It's cold outside.");
}
}
}
It's cold outside.
Explanation:
In this program, we're using a series of if-else if-else
conditions to print a message based on the current temperature.
- The value of
temperature
is 5. - The program first checks
if (temperature > 30)
– this isfalse
. - Then it checks
else if (temperature > 20)
– stillfalse
. - Next,
else if (temperature > 10)
– againfalse
. - Since none of the conditions above are true, the final
else
block executes.
Therefore, it prints: It's cold outside.
This type of conditional logic is useful for decision-making based on numeric ranges or categories.
Example 3: Nested Conditions
public class NestedIfElseIf {
public static void main(String[] args) {
int marks = 78;
char section = 'A';
if (marks >= 75) {
if (section == 'A') {
System.out.println("Eligible for Scholarship in Section A");
} else if (section == 'B') {
System.out.println("Eligible for Scholarship in Section B");
} else {
System.out.println("Eligible but section not identified");
}
} else {
System.out.println("Not eligible for scholarship");
}
}
}
Eligible for Scholarship in Section A
Explanation:
This program demonstrates how nested if-else
conditions work in Java. The outer if
checks whether the student has scored 75 or more marks. If this condition is true, the program then checks the section
to determine the specific eligibility message:
- If marks ≥ 75 and section is 'A': Prints "Eligible for Scholarship in Section A"
- If marks ≥ 75 and section is 'B': Prints "Eligible for Scholarship in Section B"
- If marks ≥ 75 but section is neither 'A' nor 'B': Prints "Eligible but section not identified"
- If marks < 75: Prints "Not eligible for scholarship" (outer
else
)
In this specific case, since marks = 78
and section = 'A'
, the output is:
Eligible for Scholarship in Section A
Common Mistakes to Avoid When Using if else if
- Using
=
instead of==
in conditions (assignment vs comparison). - Forgetting the
else
block when it's needed to handle all unexpected cases. - Overusing if-else-if instead of switch when checking for fixed values.
When to Use if-else-if Ladder
Use if-else-if
when:
- You need to check ranges (e.g., temperature, scores).
- Conditions are not fixed values but expressions.
- You want to perform different logic based on multiple distinct rules.
Summary
The if-else-if
structure in Java is all about making your program responsive to different inputs and situations. By practicing with everyday logic like grading systems, weather classifications, or eligibility checks, you build the instinct to apply this structure naturally in your code.
Try It Yourself!
Change the values in the above examples and see how the outputs differ. Experiment with adding new conditions and see where they fall in the ladder. This is the best way to gain confidence with flow control.
QUIZ
Question 1:How does Java’s if-else-if
ladder behave when multiple conditions evaluate to true
?
Question 2:In an if-else-if
structure, the else
block is mandatory and must always be included.
Question 3:Which of the following are true about the Java if-else-if
ladder?
Question 4:What will be the output of the following code?
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D or lower");
}
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D or lower");
}
Question 5:The Java if-else-if
ladder can be replaced with a switch
statement in all cases.
Question 6:Analyze the output of this program:
int temperature = 5;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else if (temperature > 20) {
System.out.println("It's a warm day.");
} else if (temperature > 10) {
System.out.println("It's a bit chilly.");
} else {
System.out.println("It's cold outside.");
}
int temperature = 5;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else if (temperature > 20) {
System.out.println("It's a warm day.");
} else if (temperature > 10) {
System.out.println("It's a bit chilly.");
} else {
System.out.println("It's cold outside.");
}