Java else if Statement - Complete Guide
Introduction to else if Statement
The else if statement in Java lets you evaluate multiple conditions in sequence. It prevents excessive nesting and improves readability by forming a chain of conditional checks.
Technically, else if is just an else followed by an if, but used together they provide a clean way to handle several exclusive conditions.
Basic else if Syntax
The else if construct checks conditions one by one until one evaluates to true. If none are true, the optional else block runs.
Example
public class ElseIfStatement {
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 if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
int hour = 14; // 2 PM
if (hour < 12) {
System.out.println("Good morning!");
} else if (hour < 18) {
System.out.println("Good afternoon!");
} else if (hour < 21) {
System.out.println("Good evening!");
} else {
System.out.println("Good night!");
}
int number = 0;
if (number > 0) {
System.out.println("Positive number");
} else if (number < 0) {
System.out.println("Negative number");
} else {
System.out.println("Zero");
}
}
}
Output
Grade: B Good afternoon! Zero
Comparison with Nested if-else
Using else if is more concise and readable than chaining nested if-else statements. Order of conditions is important: once a true condition is found, later conditions are skipped.
Example
public class ElseIfVsNested {
public static void main(String[] args) {
int score = 85;
// Nested if-else
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 {
if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
}
// Equivalent with else if
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 if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
// Order matters
int value = 75;
if (value > 50) {
System.out.println("Greater than 50");
} else if (value > 70) {
System.out.println("Greater than 70"); // skipped
}
if (value > 90) {
System.out.println("Greater than 90");
} else if (value > 70) {
System.out.println("Greater than 70");
} else if (value > 50) {
System.out.println("Greater than 50");
}
}
}
Output
Grade: B Grade: B Greater than 50 Greater than 70
Best Practices for else if Statements
- ✅ Use else if to handle multiple exclusive conditions
- ✅ Place the most specific or most likely conditions first
- ✅ Include a final else to catch unexpected values
- ✅ Avoid overlapping conditions that might cause ambiguity
- ✅ Consider switch for many equality checks
- ✅ Keep chains short for readability
- ✅ Document tricky conditions with comments
- ✅ Test all possible execution paths