Verified for the 2025 AP Computer Science A exam•Citation:
So far, we've explored if statements for one-way selection and if-else statements for two-way selection. But what happens when you need to check multiple conditions and take different actions based on which condition is true? This is where else if
statements come in, allowing us to implement multi-way selection. Multi-way selection lets us create more sophisticated decision trees in our code, enabling our programs to handle a variety of situations with elegant, readable logic.
Multi-way selection is a control structure that allows a program to choose between three or more alternative actions based on different conditions. In Java, this is accomplished using if-else-if
statements (often called an "else if ladder" or "if-else-if chain").
Important terms to know:
if (condition1) { // Code executed when condition1 is true } else if (condition2) { // Code executed when condition1 is false AND condition2 is true } else if (condition3) { // Code executed when condition1 and condition2 are false AND condition3 is true } else { // Code executed when all conditions are false }
The else if
statement combines elements of both if
and else
statements. Each else if
provides another condition to check if the previous conditions were false.
Key components:
if
statement with the first conditionelse if
clauses, each with their own conditionelse
clause that executes when all conditions are falseWhen Java encounters an if-else-if
statement:
if
)else if
)else
block executes, if present)int score = 85; char grade; if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else if (score >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Your grade is: " + grade);
In this example:
Note that only one grade is assigned, and the program executes exactly one of the code blocks.
The order of conditions in an else if
chain is critical. Java evaluates conditions from top to bottom and executes the code block for the first condition that evaluates to true.
int num = 5; // Example 1: Correct order (most specific to most general) if (num == 5) { System.out.println("Number is 5"); } else if (num > 0) { System.out.println("Number is positive"); } else { System.out.println("Number is negative or zero"); } // Output: "Number is 5" // Example 2: Problematic order (most general to most specific) if (num > 0) { System.out.println("Number is positive"); } else if (num == 5) { System.out.println("Number is 5"); // This will never execute for num = 5! } else { System.out.println("Number is negative or zero"); } // Output: "Number is positive"
In Example 2, the code block for num == 5
will never execute when num
is 5 because the first condition num > 0
already evaluates to true.
Selection Type | Structure | Use Case |
---|---|---|
One-way | if (condition) { ... } | Execute code only when a condition is true |
Two-way | if (condition) { ... } else { ... } | Choose between two alternatives |
Multi-way | if (condition1) { ... } else if (condition2) { ... } else { ... } | Choose among multiple alternatives |
int temperature = 75; if (temperature > 90) { System.out.println("It's hot! Stay hydrated and limit outdoor activities."); } else if (temperature > 70) { System.out.println("It's warm. Enjoy the pleasant weather!"); } else if (temperature > 50) { System.out.println("It's cool. Consider bringing a light jacket."); } else if (temperature > 32) { System.out.println("It's cold. Wear a coat and gloves."); } else { System.out.println("It's freezing! Stay warm and be cautious of ice."); }
double income = 65000.0; double tax; if (income <= 9950) { tax = income * 0.10; } else if (income <= 40525) { tax = 995 + (income - 9950) * 0.12; } else if (income <= 86375) { tax = 4664 + (income - 40525) * 0.22; } else if (income <= 164925) { tax = 14751 + (income - 86375) * 0.24; } else if (income <= 209425) { tax = 33603 + (income - 164925) * 0.32; } else if (income <= 523600) { tax = 47843 + (income - 209425) * 0.35; } else { tax = 157804.25 + (income - 523600) * 0.37; } System.out.println("Your estimated tax is: $" + tax);
This prevents earlier, more general conditions from "shadowing" later, more specific ones.
Either make sure your conditions cover all possibilities or include a final else
clause as a catch-all.
If conditions become complex, consider breaking them down or using helper methods.
// Instead of this: if (score >= 90 && !extraCredit && studentPresent) { // ... } // Consider this: boolean qualifiesForA = score >= 90 && !extraCredit && studentPresent; if (qualifiesForA) { // ... }
Consistent indentation makes the code structure clearer, especially with multiple conditions.
if (condition1) { // code } else if (condition2) { // code } else if (condition3) { // code } else { // code }
When checking a single variable against multiple exact values, a switch
statement might be cleaner.
int age = 16; // Problematic (less specific condition first) if (age > 0) { System.out.println("Valid age"); } else if (age >= 16) { System.out.println("Can drive"); // Never reached for age=16 } // Correct (more specific condition first) if (age >= 16) { System.out.println("Can drive"); } else if (age > 0) { System.out.println("Valid age, but too young to drive"); }
Without a final else
, your code might silently handle unexpected cases without any feedback.
int month = 15; // Invalid month // Without final else - silent failure for invalid input if (month == 12 || month <= 2) { season = "Winter"; } else if (month <= 5) { season = "Spring"; } else if (month <= 8) { season = "Summer"; } else if (month <= 11) { season = "Fall"; } // No error message for month=15! // With final else - catches unexpected input if (month == 12 || month <= 2) { season = "Winter"; } else if (month <= 5) { season = "Spring"; } else if (month <= 8) { season = "Summer"; } else if (month <= 11) { season = "Fall"; } else { System.out.println("Error: Invalid month number"); season = "Unknown"; }
Remember that in an if-else-if
chain, only the first matching condition will have its code block executed.
else if
statements enable multi-way selection, allowing code to choose between multiple pathsif-else-if
chain will execute