AP CSP Day 33: Conditionals | Cycle 2

Key Concepts

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.

Common Trap: Only testing cases that go through the IF branch. A conditional bug in the ELSE branch is invisible unless you test an input that makes the condition false.
Exam Tip: Always test at least one input that makes the condition true AND one that makes it false. Conditional bugs often only affect one branch and are invisible if you test only one case.
Big Idea 3: Algorithms & Programming
Cycle 2 • Day 33 Practice • Hard Difficulty
Focus: Conditionals

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")
}
Why This Answer?

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".

Why Not the Others?

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.

Common Mistake
Watch Out!

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.

AP Exam Tip

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
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.