Conditionals (College Board AP® Computer Science Principles): Revision Note
Conditional execution & IF statements
What is conditional execution?
Conditional execution is a form of selection: a section of code runs only if a specified condition is
trueSelection is one of the three core building blocks of all algorithms, alongside sequencing and iteration
Without selection, a program follows the same path regardless of input; conditionals allow programs to respond differently to different situations
IF statements
An IF statement evaluates a Boolean condition and executes the statements inside its block only if that condition is
trueIf the condition is
false, the block is skipped and flow control passes to the next statement after the IF block
score ← 72
IF (score ≥ 50)
{
DISPLAY("Pass")
}
If
scoreis 72, the condition72 ≥ 50istrue, so"Pass"is displayedIf
scorewere 40, the condition would befalseand nothing would be displayed
Branching & nested conditionals
What is branching logic?
An IF-ELSE statement creates two branches: one that executes when the condition is
true, and one that executes when the condition isfalseExactly one branch always executes; unlike a plain IF statement, an IF-ELSE guarantees an outcome in both cases
IF (score ≥ 50)
{
DISPLAY("Pass")
}
ELSE
{
DISPLAY("Fail")
}
Structure | When to use | Guarantee of output |
|---|---|---|
| When an action is only needed if the condition is | No guarantee — if the condition is |
| When a different action is needed for both | Always produces output — exactly one branch executes regardless of the condition |
Nested conditionals
A nested conditional is an IF or IF-ELSE statement placed inside another IF or IF-ELSE block
Nested evaluation works from the outside in: the outer condition is checked first; the inner condition is only evaluated if the outer condition is
trueNested conditionals allow programs to test multiple conditions in sequence
IF (score ≥ 50)
{
IF (score ≥ 90)
{
DISPLAY("Honors pass")
}
ELSE
{
DISPLAY("Pass")
}
}
ELSE
{
DISPLAY("Fail")
}
The inner conditions are only reached if the outer condition (
score ≥ 50) is alreadytrueA score of 95 displays
"Honors pass", a score of 72 displays"Pass", and a score of 40 displays"Fail"
Examiner Tips and Tricks
Use IF when only one outcome needs code — the false case requires no action. Use IF-ELSE when both outcomes require different code. When tracing nested conditionals, evaluate the outermost condition first and only move inward if that condition is
true.For the AP Create Performance Task, your program must include selection — an IF or IF-ELSE statement with a meaningful condition that affects your program's behavior. Be prepared to explain why your condition is necessary and what would happen without it.
Worked Example
A program contains the following code segment.
x ← 15
IF (x > 10)
{
IF (x > 20)
{
DISPLAY("Large")
}
ELSE
{
DISPLAY("Medium")
}
}
ELSE
{
DISPLAY("Small")
}
What is displayed when this code segment is executed?
(A) Large
(B) Medium
(C) Small
(D) Nothing is displayed
[1]
Answer:
(B) Medium [1 mark]
x > 10istrueso the inner conditional is reached;x > 20isfalseso the ELSE branch executes and displays"Medium"
Unlock more, it's free!
Was this revision note helpful?