Java else-if Statement
The else-if statement in Java is a powerful extension of the basic if-else structure that allows your program to evaluate multiple conditions sequentially. It helps you make decisions among several possible outcomes instead of just two (true or false).
When you have a situation where different actions must be taken depending on various conditions, using multiple if-else statements can make your code messy. The else-if ladder provides a clean and efficient way to handle such cases.
Syntax of the else-if Statement
if (condition1) {
// Executes if condition1 is true
} else if (condition2) {
// Executes if condition2 is true
} else if (condition3) {
// Executes if condition3 is true
} else {
// Executes if none of the above conditions are true
}
Here’s how it works:
- The program starts checking from the first condition.
- If a condition evaluates to true, its block is executed, and the rest are skipped.
- If none of the conditions are true, the else block executes (if present).
Example 1: Grading System
public class GradeSystem {
public static void main(String[] args) {
int marks = 82;
if (marks >= 90) {
System.out.println(“Grade: A+”);
} else if (marks >= 80) {
System.out.println(“Grade: A”);
} else if (marks >= 70) {
System.out.println(“Grade: B”);
} else if (marks >= 60) {
System.out.println(“Grade: C”);
} else {
System.out.println(“Grade: F”);
}
}
}
Output:
Grade: A
Explanation:
The program checks each condition in sequence. Since marks = 82, the second condition (marks >= 80) is true, so “Grade: A” is printed. All subsequent conditions are ignored.
Example 2: Temperature Check
public class TemperatureCheck {
public static void main(String[] args) {
int temperature = 15;
if (temperature > 35) {
System.out.println(“It’s a hot day!”);
} else if (temperature >= 25) {
System.out.println(“It’s a warm day!”);
} else if (temperature >= 15) {
System.out.println(“It’s a pleasant day!”);
} else {
System.out.println(“It’s a cold day!”);
}
}
}
Output:
It’s a pleasant day!
Explanation:
Here, the temperature is 15, which satisfies the third condition (temperature >= 15). Therefore, “It’s a pleasant day!” is printed.
Example 3: Determining the Largest Number
public class LargestNumber {
public static void main(String[] args) {
int a = 10, b = 25, c = 20;
if (a > b && a > c) {
System.out.println(“a is the largest number.”);
} else if (b > a && b > c) {
System.out.println(“b is the largest number.”);
} else {
System.out.println(“c is the largest number.”);
}
}
}
Output:
b is the largest number.
Explanation:
The program compares three numbers using logical operators. The second condition (b > a && b > c) is true, so the message indicating that b is the largest number is displayed.
Example 4: Checking Exam Result
public class ExamResult {
public static void main(String[] args) {
int score = 55;
if (score >= 90) {
System.out.println(“Excellent!”);
} else if (score >= 75) {
System.out.println(“Very Good!”);
} else if (score >= 50) {
System.out.println(“Pass”);
} else {
System.out.println(“Fail”);
}
}
}
Output:
Pass
Explanation:
Since score = 55, the first two conditions are false, and the third condition (score >= 50) is true. Hence, “Pass” is printed.
Key Points to Remember:
- The else-if statement must always follow an if statement.
- You can have multiple else-if conditions, but only one else block at the end.
- Once a condition is true, the remaining conditions are not evaluated.
- The else block is optional but recommended for handling unexpected or default cases.
Each condition must be a boolean expression that evaluates to true or false.
Example 5: Nested else-if (Combining Multiple Criteria)
public class DiscountCalculator {
public static void main(String[] args) {
double purchaseAmount = 4500;
if (purchaseAmount >= 5000) {
System.out.println(“You get a 20% discount!”);
} else if (purchaseAmount >= 3000) {
System.out.println(“You get a 15% discount!”);
} else if (purchaseAmount >= 1000) {
System.out.println(“You get a 10% discount!”);
} else {
System.out.println(“No discount available.”);
}
}
}
Output:
You get a 15% discount!
Explanation:
Since purchaseAmount = 4500, the program checks each condition in order. The second condition (purchaseAmount >= 3000) is true, so it prints the corresponding discount message.
Advantages of Using else-if
- Makes code cleaner and easier to read than multiple nested if statements.
- Prevents unnecessary checks once a condition is met.
- Ideal for scenarios with mutually exclusive conditions, such as grading systems, temperature ranges, or category selections.
Conclusion
The else-if statement in Java is a simple yet powerful tool for handling multi-branch decisions efficiently. It ensures that your program executes only one block of code among several possible options, based on the first true condition. By mastering else-if, you can write programs that make intelligent and context-aware decisions, improving readability and reducing redundancy in your code.