How does an else if chain select one outcome from several mutually exclusive options?
Topic 3.4 else if Statements: use an if / else if / else chain so that the first true condition runs its block and the rest are skipped, selecting exactly one outcome.
A focused answer to AP CSA Topic 3.4, covering the if / else if / else chain, why the first true condition wins and later ones are skipped, why ordering matters, the role of the final else, and how to trace a multi-branch decision, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 3.4) wants you to write and trace an if / else if / else chain: a multi-way decision that selects exactly one outcome from several mutually exclusive options. Java tests the conditions in order from the top; the first one that is true runs its block, and every remaining branch is skipped. Getting the ordering right and knowing when later branches are unreachable is the key skill.
Syntax of the chain
if (c1) {
// block A
} else if (c2) {
// block B
} else if (c3) {
// block C
} else {
// block D, the catch-all
}
This is really nested if-else written compactly: each else if is an if placed in the else of the one above it. The flat layout just makes long chains readable.
First true condition wins
This "first match wins" behavior means a later condition can be unreachable if an earlier one already covers its cases. In the grade example, once score >= 80 is tested, reaching it guarantees score < 90, so you do not need to write score >= 80 && score < 90.
Order matters
If you reversed the grade chain to test score >= 70 first, every score of 70 or more would print C, because that broad condition would match before the stricter ones were reached. Always order from most restrictive to least restrictive.
The final else is optional
The closing else is a catch-all that runs when no condition above it was true. It is optional: a chain of if / else if with no final else simply does nothing when none of the conditions hold. Include the else whenever every input should produce an outcome.
Try this
Q1. In an if / else if / else if chain, how many blocks can run on one pass? [1 point]
- Cue. At most one - the first whose condition is
true(or the finalelseif none match).
Q2. Explain why ordering the conditions wrongly can break an else if chain. [2 points]
- Cue. The first true condition wins, so a broad condition placed before a stricter one captures cases meant for the later branch, making it unreachable.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following code segment.
```java
int score = 85;
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "F";
}
System.out.println(grade);
```
What is printed?
(A) `B`
(B) `A`
(C) `C`
(D) `F`
(E) `BC`
Show worked answer →
The answer is (A).
The chain tests conditions top to bottom and stops at the first that is true. score >= 90 is 85 >= 90, which is false, so move on. score >= 80 is 85 >= 80, which is true, so grade = "B" runs and the rest of the chain is skipped - the score >= 70 test is never reached even though it would also be true. (C) wrongly continues past the first match; (E) wrongly runs two blocks.
Markers reward knowing that the first true condition wins and every later branch (including the else) is skipped.
AP 2020 (style)4 marksFree response (code writing). An `int` variable `n` is given. Write a code segment using an if / else if / else chain that prints `"negative"` if `n` is less than 0, `"zero"` if `n` equals 0, and `"positive"` otherwise. Print exactly one word.
Show worked answer →
A 4-point question testing a multi-way decision.
if (n < 0) {
System.out.println("negative");
} else if (n == 0) {
System.out.println("zero");
} else {
System.out.println("positive");
}
Point 1: the first condition n < 0 handles negatives. Point 2: the else if (n == 0) is only reached when n is not negative, so it correctly isolates zero. Point 3: the final else is the catch-all for positives, needing no further test. Point 4: exactly one word prints because the chain stops at the first true branch. Replacing the chain with three independent if statements would still work here but is less efficient and easier to get wrong.
Related dot points
- Topic 3.3 if-else Statements: use a two-way if-else statement so that exactly one of two code blocks runs depending on whether the boolean condition is true or false.
A focused answer to AP CSA Topic 3.3, covering two-way selection with if-else, the guarantee that exactly one branch runs, how the else attaches to the nearest if, nested if-else, and how to trace each branch, with a fully worked example.
- Topic 3.2 if Statements and Control Flow: use a one-way if statement so that a block of code runs only when its boolean condition is true, and trace the resulting flow of control.
A focused answer to AP CSA Topic 3.2, covering the syntax of a one-way if statement, the role of the boolean condition, why braces matter, the dangling-statement trap, and how control flows through and past the if, with a fully traced worked example.
- Topic 3.1 Boolean Expressions: evaluate expressions formed with the relational operators (<, >, <=, >=, ==, !=) that produce a boolean result of true or false.
A focused answer to AP CSA Topic 3.1, covering the six relational operators, how they compare numeric primitives to produce a boolean, the difference between == and =, comparing doubles, and how relational operators combine with arithmetic, with a fully traced worked evaluation.
- Topic 3.5 Compound Boolean Expressions: combine boolean expressions with the logical operators && (and), || (or) and ! (not), applying short-circuit evaluation.
A focused answer to AP CSA Topic 3.5, covering the logical operators && , || and !, their truth tables, operator precedence and short-circuit evaluation, why short-circuiting prevents errors, and how to trace a compound condition, with a fully worked example.
- Topic 3.6 Equivalent Boolean Expressions: apply De Morgan's laws and truth tables to produce equivalent boolean expressions and to simplify negations of compound conditions.
A focused answer to AP CSA Topic 3.6, covering De Morgan's laws for negating && and ||, using truth tables to prove equivalence, simplifying double negation, and rewriting conditions to remove a leading !, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)