Unit 2 Cycle 1 Day 6: Multi-Branch Else-If
Share
Multi-Branch Else-If
Section 2.4 — Else-If Statements
Key Concept
An else-if chain evaluates conditions from top to bottom and executes only the first matching branch. Once a condition is true, all remaining else-if and else blocks are skipped entirely. This means the order of conditions matters: placing a broader condition before a narrower one can prevent the narrower condition from ever being reached. The AP exam tests whether rearranging conditions changes the output, and whether you understand that at most one branch in the chain executes.
Consider the following code segment.
What is printed as a result of executing the code segment?
Answer: (C) warm
temp = 75. Check: 75 > 100 false. Check: 75 > 85 false. Check: 75 > 70 true. Assigns "warm" and skips the else.
Why Not the Others?
(A) 75 is not greater than 100.
(B) 75 is not greater than 85.
(D) 75 IS greater than 70, so the else branch is never reached.
Common Mistake
In an else-if chain, only the FIRST true condition executes. Once a match is found, all remaining branches are skipped. The order of conditions matters.
AP Exam Tip
Else-if chains should be ordered from most restrictive to least restrictive (or vice versa, depending on logic). Each condition assumes all previous conditions were false.