Sub topic 3
Test Multiple Conditions in the Right Order
Suppose a score of 90 or more should earn “Excellent,” a score from 70 to 89 should earn “Pass,” and a lower score should earn “Keep practicing.” You want to print just one message. An else-if chain lets you list these choices in order and stop at the first one that fits.
Java Stops at the First True Condition
A condition is a test that gives either true or false. A branch is a path the program can take. In an else-if chain, each branch has a block, which is a group of statements inside braces.
Java starts with the condition after if. If that condition is true, Java runs its block and skips the rest of the chain. If it is false, Java moves to the next condition, written after else if. You can add more than one else if. Each one is tested only if every condition above it was false.
The final else has no condition. It runs only when none of the earlier conditions were true. After the selected block runs, the program continues after the whole chain.
Work Through a Score of 85
Save this program as Main.java and run it. The symbol >= means “greater than or equal to.”
public class Main {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 70) {
System.out.println("Pass");
} else {
System.out.println("Keep practicing");
}
}
}The program prints “Pass.” To trace the chain, follow the tests in the same order Java does:
- Is 85 at least 90? No. Java skips the first block.
- Is 85 at least 70? Yes. Java prints “Pass.”
- Java leaves the chain. It does not run the final else.
You might expect the “Pass” condition to check both ends of the range: at least 70 and below 90. The upper check is not needed here. By the time Java gets to else if, it already knows the score is below 90. The earlier test did that work.
Put the Narrower Test First When Conditions Overlap
Two conditions overlap when the same value can make both true. A score of 95 is at least 90, but it is also at least 70. In the program above, “Excellent” wins because Java checks for it first. Java does not even test whether 95 is at least 70.
Now imagine moving the “Pass” branch to the top, with its condition of at least 70. For a score of 95, that test is true, so Java prints “Pass” and skips the “Excellent” test. In fact, no score would reach the “Excellent” block in that order. Every score high enough for “Excellent” would already match “Pass.”
This is also why two separate if statements are not the same as an else-if chain. With separate statements, Java tests both conditions. A score of 95 could print both “Excellent” and “Pass.” Keep the tests in one chain when you want just one of those messages.
Test the Values Where the Result Changes
A cutoff is a value where the result changes. Here, the cutoffs are 70 and 90. Change the starting score in the program and run it once for each value in the table. Try to predict the message before you run it.
| Score | Expected message | Why |
|---|---|---|
| 69 | Keep practicing | Neither test is true. |
| 70 | Pass | The second test includes 70. |
| 89 | Pass | The first test is false, but the second is true. |
| 90 | Excellent | The first test includes 90. |
| 95 | Excellent | The first test wins even though both tests could be true. |
Testing just below and right at each cutoff helps catch a common error: using >, which means “greater than,” instead of >=. If you use > for the first test, 91 still earns “Excellent,” but 90 incorrectly earns “Pass.”
Decide What Happens When Nothing Matches
The final else is optional. Keep it when every input should get a result, as in the score program. If the program should only report scores of 70 or more, remove the final else block. Then a score of 69 runs no block in the chain, and Java continues with whatever code comes next.
Practice
Exercise
You move the “at least 70” branch above the “at least 90” branch. What does the program print for 92, and why?
Show answer
It prints “Pass.” The first test is true, so Java runs that block and skips the rest of the chain.
Exercise
In the original program, you change only the second test from “at least 70” to “greater than 70.” What does the program print for exactly 70?
Show answer
It prints “Keep practicing.” The score is below 90 and is not greater than 70, so both tests are false.