Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Syntax of the chain
  3. First true condition wins
  4. Order matters
  5. The final else is optional
  6. Try this

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.

flowchart TD A{c1?} -->|true| B[block A] A -->|false| C{c2?} C -->|true| D[block B] C -->|false| E{c3?} E -->|true| F[block C] E -->|false| G[else block D]

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 final else if 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

Sources & how we know this