AP CSP Day 55: Conditionals | Cycle 2
Share
Reachable versus unreachable code is a conditional logic issue where a condition can never be true given the constraints established by earlier conditions. For example, after an IF block that handles x > 10, an ELSE IF block checking x > 20 is unreachable because x > 20 implies x > 10, which already executed. AP CSP Cycle 2 conditional review questions ask students to identify which branch of a multi-condition structure is unreachable and explain why. Recognizing overlapping conditions that make later branches dead code demonstrates a mastery-level understanding of conditional logic.
📚 Study the Concept First (Optional) Click to expand ▼
Unreachable Code and Dead Branches
What Is Unreachable Code?
A branch of a conditional is unreachable if no possible input value can make its condition true given the conditions checked before it. This creates dead code: lines that are syntactically valid but can never execute.
Example of a Dead Branch
If the first condition is x > 10 and its ELSE IF condition is x > 20, the ELSE IF is dead. Any value where x > 20 is also where x > 10, so the first branch always executes instead.
Practice Question
What is displayed after the following code runs?
x ← 7
y ← 3
z ← 5
IF x > y AND y > z
{
DISPLAY("A")
}
ELSE
{
IF x > z OR y > z
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}First condition: x > y AND y > z = (7 > 3) AND (3 > 5) = true AND false = false. The ELSE block executes. Inside ELSE: x > z OR y > z = (7 > 5) OR (3 > 5) = true OR false = true. "B" is displayed.
A) The AND condition fails because y (3) is not greater than z (5). C) The OR condition is true because x (7) is greater than z (5). B) Only one branch of each IF/ELSE pair executes.
Students evaluate the AND condition by checking only the first part (x > y = true) and assume the entire condition is true without checking the second part (y > z = false).
For compound Boolean conditions with AND, both parts must be true. For OR, only one part needs to be true. Check each part separately before combining.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help