Unit 2 Cycle 2 Day 6: Tricky Else-If vs Sequential If
Share
Tricky Else-If vs Sequential If
Section 2.4 — Else-If Statements
Key Concept
An else-if chain is not the same as a sequence of independent if statements. In an else-if chain, only the first true condition's block executes. With sequential if statements, multiple blocks can execute if their conditions are independently true. The AP exam presents both versions and asks whether they produce the same output. They are equivalent only when the conditions are mutually exclusive (no two can be true simultaneously). If conditions overlap, sequential if statements may execute multiple blocks.
Consider the following code segment.
What is printed as a result of executing the code segment?
Answer: (B) ABC
These are three separate if statements, NOT an else-if chain. score=95: 95 >= 90 true, append "A". 95 >= 80 true, append "B". 95 >= 70 true, append "C". Result: "ABC".
Why Not the Others?
(A) "A" would be correct if these were else-if statements (only first match executes). But separate ifs all execute independently.
(C) All three conditions are true for score=95, so all three append operations execute.
(D) "C" would be the result only if score were 70-79.
Common Mistake
Sequential ifs are all independent and ALL execute. An else-if chain stops at the first true condition. This is one of the most commonly tested distinctions on the AP exam.
AP Exam Tip
Look carefully at the structure: if-if-if (all checked) vs if-else if-else if (first match only). The presence or absence of else completely changes behavior.