AP CSP Day 33: Conditionals | Cycle 2
Share
Conditional logic errors include using the wrong comparison operator, inverting a condition, or placing a condition in the wrong branch of an IF/ELSE structure. A common bug is writing a strictly greater-than comparison (>) when greater-than-or-equal-to (>=) is required, causing the boundary case to execute the wrong branch. AP CSP Cycle 2 conditional questions present flawed pseudocode and ask students to identify which input value reveals the bug. Testing boundary values systematically is the most reliable strategy for exposing conditional errors.
📚 Study the Concept First (Optional) Click to expand ▼
Conditional Bugs: Wrong Branch Executes
Inverted Condition Bug
An inverted condition uses the opposite comparison operator from what was intended, causing the program to execute the wrong branch for every input. This is one of the hardest bugs to spot because the code structure looks correct.
Wrong Else Assignment
In an IF/ELSE structure, putting the correct action in the wrong branch is a logic error that only appears when inputs trigger the affected branch. The code may work correctly for most test cases and fail only on the boundary.
Practice Question
What is displayed after the following code runs?
x ← 15
y ← 10
IF x > y
{
IF x - y > 10
{
DISPLAY("far apart")
}
ELSE
{
DISPLAY("close")
}
}
ELSE
{
DISPLAY("y is bigger")
}First condition: x > y evaluates to 15 > 10 = true. Inside the nested IF: x - y > 10 evaluates to 15 - 10 = 5 > 10 = false. The ELSE branch executes, displaying "close".
A) The difference (5) is not greater than 10. C) The outer condition x > y is true, so the outer ELSE does not execute. B) One of the branches must execute because the outer IF/ELSE covers all cases.
Students evaluate the outer condition correctly but then rush through the nested condition without computing the actual difference, or they confuse which ELSE pairs with which IF.
In nested IF/ELSE structures, work from the outside in. Determine which outer branch executes first, then evaluate the inner conditions within that branch only.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help