If Else If
If Else If
A school program needs reliable data and clear rules. An if-else-if chain checks conditions in order and runs the first matching branch. Test the highest grade threshold first so that it is not swallowed by a lower threshold.
Understand
An if-else-if chain checks conditions in order and runs the first matching branch.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Putting marks >= 50 first would also catch 85. A later branch is not checked after an earlier match.
if else if in Java
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, theelseblock runs (if provided).
Flow diagram: key actions and decisions
- Start
- condition1?
- code block 1
- condition2?
- code block 2
- condition3?
- code block 3
- default block
- End
These labels identify the actions and decisions in the original diagram; the list does not represent branch order. Follow the Java code for the exact execution path.
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
temperatureis 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
elseblock 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
elseblock 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.
Important boundaries and mistakes
Putting marks >= 50 first would also catch 85. A later branch is not checked after an earlier match.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Putting marks >= 50 first would also catch 85.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
int marks = 85;
if (marks >= 80) {
System.out.println("A");
}
else if (marks >= 50) {
System.out.println("Pass");
}
else {
System.out.println("Practise");
}
}
}
Now change one thing: Change marks to 60. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
int marks = 85;
if (marks >= 80) {
System.out.println("A");
}
else if (marks >= 50) {
System.out.println("Pass");
}
else {
System.out.println("Practise");
}
}
}
Expected output
AAn if-else-if chain checks conditions in order and runs the first matching branch. Test the highest grade threshold first so that it is not swallowed by a lower threshold.
Putting marks >= 50 first would also catch 85. A later branch is not checked after an earlier match.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Worked variation: Change marks to 60. The highest threshold is false and the next branch prints Pass.
Check your understanding
What to remember
An if-else-if chain checks conditions in order and runs the first matching branch. Test the highest grade threshold first so that it is not swallowed by a lower threshold. Putting marks >= 50 first would also catch 85. A later branch is not checked after an earlier match.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.