AP CSP Day 3: Conditionals
Share
Conditional statements (IF, IF/ELSE) allow programs to make decisions based on Boolean conditions at runtime. The body of an IF block executes only when the condition evaluates to true; an ELSE block executes when it is false. AP CSP exam questions frequently present nested conditionals where only one branch executes, and students must identify which output or variable value results from a given input. Reading every condition carefully before tracing is essential.
📚 Study the Concept First (Optional) Click to expand ▼
Conditional Statements: IF and IF/ELSE
How Conditionals Work
A conditional lets a program take different actions based on whether a condition is true or false. The IF block runs only when the condition is true. The ELSE block runs when the condition is false. Only one branch ever executes for a given input.
Real-World Analogy
A thermostat: IF the temperature drops below 68, turn on the heat. ELSE, leave it off. The thermostat checks the condition once and acts accordingly every time.
Practice Question
What is displayed after the following code runs?
score ← 85
IF score ≥ 90
{
DISPLAY("A")
}
ELSE
{
IF score ≥ 80
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}score is 85. The first condition (85 ≥ 90) is false, so execution enters the ELSE block. Inside the ELSE, the condition 85 ≥ 80 is true, so "B" is displayed. The inner ELSE block ("C") is skipped.
A) 85 is not ≥ 90, so the first IF is false. B) The inner condition 85 ≥ 80 is true, so the inner ELSE does not execute. D) In an IF/ELSE structure, only one branch executes — never both.
Students sometimes evaluate all conditions independently instead of recognizing that IF/ELSE chains stop at the first true condition. Once a branch executes, all remaining branches are skipped.
In IF/ELSE chains, evaluate conditions from top to bottom and stop at the first true condition. Only one branch in an IF/ELSE structure will ever execute.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help