Unit 2 Cycle 2 Day 13: I/II/III: If-Else Outcomes
Share
I/II/III: If-Else Outcomes
Section 2.4 — Else-If Statements
Key Concept
I/II/III if-else outcome questions present a code structure with conditionals and ask which statements about the possible outputs are true. The strategy is to consider all possible input scenarios that could reach each branch. If a statement claims a certain output is possible, you need to find at least one input that produces it. If a statement claims something always or never happens, you need to verify across all possible inputs. Consider boundary values and special cases that students often overlook.
Consider the following code segment where n is a positive integer.
Which values of n result in result being "Z"?
Answer: (A) n = 3, 7, 11, 15
The else branch executes when n % 4 is not 0, 1, or 2, meaning n % 4 == 3. Numbers where n % 4 == 3: 3, 7, 11, 15, 19, ...
Why Not the Others?
(B) n % 4 == 0 gives "W": 4, 8, 12, 16.
(C) n % 4 == 2 gives "Y": 2, 6, 10, 14.
(D) n % 4 == 1 gives "X": 1, 5, 9, 13.
Common Mistake
The else branch catches all remaining cases. Since n % 4 produces 0, 1, 2, or 3, and the if-else if handles 0, 1, 2, the else catches only n % 4 == 3.
AP Exam Tip
When an else-if chain tests n % k for specific remainders, the else branch handles whatever remainders are not explicitly listed.