Topic 3.6: Conditionals | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.6 Conditionals
3.6
Big Idea 3 • Algorithms & Programming

Conditionals

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Branch Predictor game 💻 Live Python editor AAP-2.G • AAP-2.H

After this lesson, you will be able to:

  • Write a one-way selection (IF) and a two-way selection (IF/ELSE) in Python and AP pseudocode
  • Build conditions using relational operators and the logical operators NOT, AND, and OR
  • Avoid the number-one Python conditional trap: using = when you need ==
  • Explain why two separate IF statements can behave differently from a single IF/ELSE
  • Trace conditional code to determine exactly which branch runs and what is displayed
  • Identify a meaningful selection statement for your Create Performance Task written response
📈 Exam weight: Selection is one of the most frequently tested ideas in Big Idea 3, which is 30 to 35 percent of the exam. Conditional questions show up as tracing, spot-the-bug, and Boolean-logic items, and selection is a required feature of your Create Task program. This topic pays off twice.
💡 Think about this first

Every program that reacts to you is running conditionals. Your phone decides "low battery" below 20 percent. A game decides "game over" when lives reach zero. A login decides "welcome back" or "wrong password." Without selection, a program does the exact same thing every time no matter the input. Conditionals are how a program makes a decision, and learning to predict which decision it makes is the core skill this lesson builds.

One-Way Selection: the IF Statement

A conditional statement (an "if statement") runs a block of code only when a condition is true. The condition is a Boolean expression, so it evaluates to either true or false. A one-way selection has a single IF and no ELSE: when the condition is true the block runs, and when it is false nothing happens and the program simply moves on.

battery = 15
if battery < 20:
    print("low battery")
print("done")
battery <- 15
IF (battery < 20)
{
    DISPLAY("low battery")
}
DISPLAY("done")

Here battery is 15, so battery < 20 is true and the program prints low battery and then done. If battery were 80, the condition would be false, the indented block would be skipped, and only done would print. Notice that in Python the block is marked by indentation, while AP pseudocode uses curly braces. Both show exactly which statements belong to the IF.

🎯 What the exam reference sheet gives you

On the exam, one-way selection is written IF(condition) { }. The block runs only if the Boolean expression is true, and no action is taken if it is false. You never memorize syntax for the exam, you read it. Your job is to determine the result.

✍ Mini Exercise 1 • Predict the output
The variable is deliberately named k so it tells you nothing. What does this print?
k = 7
if k > 10:
    k = 0
print(k)

Two-Way Selection: IF / ELSE

A two-way selection adds an ELSE. Now the program always does exactly one of two things: the IF block when the condition is true, or the ELSE block when it is false. There is no way for both to run and no way for neither to run. Exactly one branch executes, every time.

code = 4321
if code == 4321:
    print("unlocked")
else:
    print("locked")
code <- 4321
IF (code = 4321)
{
    DISPLAY("unlocked")
}
ELSE
{
    DISPLAY("locked")
}
⚠ The single biggest Python conditional trap

Look closely at the two panels above. AP pseudocode tests equality with a single =. Python tests equality with a double ==. In Python a single = means assignment, so if code = 4321: is a syntax error and the program will not run at all. When you move from reading pseudocode to writing Python for the Create Task, this is the mistake that breaks the most programs.

✍ Mini Exercise 2 • Spot the error
This Python is meant to compare two values. Which statement about it is true?
a = 3
b = 3
if a = b:
    print("equal")

Building the Condition

The power of a conditional is in its condition. A condition is any Boolean expression, and you build it from two families of operators from the exam reference sheet.

Relational operators

Relational operators compare two values and produce a Boolean. The exam reference sheet lists six: =, , >, <, , and . In Python these become ==, !=, >, <, >=, and <=. The two you must watch are the boundary pairs: > versus and < versus . A single wrong boundary is the most common conditional bug on the exam, and it fails for exactly one input value.

Logical operators: NOT, AND, OR

Logical operators combine or flip Boolean values. NOT reverses a condition. AND is true only when both sides are true. OR is true when at least one side is true. Mixing up AND and OR is the second most common conditional error, because an OR is far easier to satisfy than an AND.

age = 70
# kids under 13 OR seniors 65+
if age < 13 or age >= 65:
    print("half price")
else:
    print("full price")
age <- 70
// kids under 13 OR seniors 65+
IF (age < 13 OR age >= 65)
{
    DISPLAY("half price")
}
ELSE
{
    DISPLAY("full price")
}

MOD for divisibility (even, odd, multiples)

A condition often needs to know if a number divides evenly. The MOD operator (written % in Python) gives the remainder after division. n MOD 2 is 0 when n is even and 1 when n is odd, so n MOD 2 = 0 is the standard test for "even." More generally, n MOD d = 0 means n is a multiple of d. You will use this constantly in conditions.

✍ Mini Exercise 3 • Fill in the blank
A value t is set below. Type the exact word this prints (lowercase).
t = 6
if t < 3 or t > 6:
    print("out")
else:
    print("in")
prints:

Two Separate IFs Are Not the Same as IF / ELSE

This distinction is a favorite exam trap. With IF/ELSE, the two blocks are mutually exclusive, so exactly one runs. With two separate IF statements, each condition is checked on its own, so zero, one, or both blocks can run. The difference becomes visible whenever the first block changes a variable that the second condition then tests.

n = 6
if n > 5:
    n = n - 4
if n > 5:
    n = n + 10
print(n)   # 2
n <- 6
IF (n > 5)
{
    n <- n - 4
}
ELSE
{
    n <- n + 10
}
DISPLAY(n)   // 2

In the two-IF version on the left, n starts at 6, the first IF is true so n becomes 2, and then the second IF checks the new value: 2 is not greater than 5, so it is skipped and the result is 2. In this specific case the IF/ELSE version also gives 2, because only the first condition was ever true. But change the starting value and they diverge: for a value where the first block pushes n back above 5, the two-IF version runs the second block too, while IF/ELSE never would. The lesson: with separate IFs, always re-check each condition using the most current value.

🎯 Exam habit

When you see two IF statements in a row, do not assume they are exclusive. Trace them one at a time and update your variable table after each block. The moment the first block changes a variable, the second condition may flip.

How to Trace a Conditional

Tracing selection is a repeatable procedure. Do it the same way every time and these questions stop being guesswork:

  1. Write down the current value of every variable the condition uses.
  2. Substitute those values into the Boolean expression and evaluate it to true or false. Evaluate NOT first, then AND, then OR.
  3. If the condition is true, run the IF block. If it is false, run the ELSE block, or skip entirely when there is no ELSE.
  4. Update your variable table with any changes the block made.
  5. Continue to the next statement. If it is another IF, start over at step 1 with the updated values.
⚠ Four bugs the exam hides in conditionals

Watch for these on every tracing item: a wrong boundary (> where was needed), the wrong logical operator (OR where AND was meant), swapped branch bodies (the IF and ELSE actions traded places), and a single = used where equality == was intended in Python. Each one fails in a narrow, predictable way, which is exactly why the exam uses them.

✍ Mini Exercise 4 • Which value breaks it
This is meant to print discount when c is at least 100. For which value of c does it give the wrong answer?
if c > 100:
    print("discount")
else:
    print("full price")

Key Vocabulary

Term Definition Example
Selection Choosing which part of an algorithm runs based on whether a condition is true or false IF/ELSE
Condition A Boolean expression that evaluates to true or false and controls the branch score >= 60
One-way selection A single IF with no ELSE; the block runs only when the condition is true if x > 0:
Two-way selection IF/ELSE; exactly one of the two blocks always runs if / else
Relational operator Compares two values and returns a Boolean: = ≠ > < ≥ ≤ a ≠ b
Logical operator NOT, AND, OR; combines or flips Boolean values x > 0 AND x < 9
== vs = In Python, == tests equality and = assigns; a condition needs == if a == b:
MOD (%) Remainder after division; n MOD 2 = 0 tests for even n % 2 == 0
📋 Create Performance Task • How selection earns points

Selection is not optional on the Create Task. Your program must use at least one conditional, and your written response has to point to it and explain it. Graders are looking for a specific, accurate description, not a vague mention. Conditionals are also the natural place to show your program "manages complexity" and "handles varied input," which are the exact phrases the rubric rewards.

What a point-earning answer looks like

Suppose your Create Task is a workout tracker and this is your selection:

minutes = 45 IF (minutes >= 30) { DISPLAY("goal met") } ELSE { DISPLAY("keep going") }

A shallow response ("I used an if statement to check minutes") earns nothing. A response that earns the point identifies the statement, states the condition, describes both branches, and ties it to the program's purpose:

  • "The selection statement tests the condition minutes >= 30."
  • "When the condition is true, the program displays goal met; when it is false, it displays keep going."
  • "This selection lets the program respond differently to different input values, which is how it gives the user meaningful feedback instead of the same message every time."

The reachability rule that quietly costs points

Both branches must be meaningful and reachable. If one branch can never run for any valid input, your conditional does not really demonstrate selection. For example, IF (age >= 0) around your whole program is technically a conditional, but since age is never negative the ELSE never runs, so it earns nothing. Choose a condition that genuinely can go both ways for the inputs your program accepts. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Spot the bug
Predict first: read the code and decide what happens before you look at the choices.
What is the result of running this code segment?
m = 4 n = 5 if m = n: print("same") else: print("different")
Incorrect. The code never runs. if m = n: uses a single = (assignment) where a condition needs the == equality operator, so Python raises a syntax error before any output.
Incorrect. It cannot print anything: m = n inside the condition is assignment, not comparison, and Python rejects it as a syntax error.
Correct. A condition must be a comparison. m = n is assignment, so Python raises a SyntaxError and nothing prints. The fix is if m == n:.
Incorrect. IF/ELSE can only run one branch, and in any case this segment errors out before running because of the single = in the condition.
Question 2 of 6I, II, III
The variable k holds a positive integer. Which of the following conditions evaluate to true exactly when k is an even number greater than 10?
  • I. k > 10 AND k MOD 2 = 0
  • II. NOT (k ≤ 10 OR k MOD 2 = 1)
  • III. k > 10 AND k MOD 2 = 1
Incorrect. I is correct, but II is also correct. By De Morgan, NOT (k ≤ 10 OR k MOD 2 = 1) becomes (k > 10) AND (k MOD 2 = 0), which is identical to I.
Correct. I directly says "greater than 10 and even." II is the same statement rewritten with NOT and De Morgan: NOT (k ≤ 10 OR k odd) = (k > 10) AND (k even). III fails because k MOD 2 = 1 means odd, not even.
Incorrect. III tests for an ODD number greater than 10 (k MOD 2 = 1), which is the opposite of what is asked. II is correct, but it must pair with I, not III.
Incorrect. III is wrong: k MOD 2 = 1 selects odd numbers. Only I and II describe "even and greater than 10."
Question 3 of 6Trace
Predict first: trace line by line with a variable table before checking the options.
What does this code segment display?
v = 6 if v > 5: v = v - 4 if v > 5: v = v + 10 print(v)
Correct. v = 6, first IF is true so v becomes 2. The second IF re-checks the NEW value: 2 > 5 is false, so it is skipped. Final v is 2.
Incorrect. 12 assumes the second IF still saw the old value 6. But after the first block v is 2, and 2 > 5 is false, so the second block never runs.
Incorrect. 16 would require both blocks to run on the original 6. These are separate IFs, so the second one tests the updated value 2 and is skipped.
Incorrect. The first IF is true (6 > 5), so v is changed to 2. It does not stay 6.
Question 4 of 6NOT question
In each segment below, s has been assigned the value 70. Which segment does NOT display approved?
Incorrect. 70 >= 70 is true, so this DOES display approved. The question asks for the one that does not.
Correct. This uses strict >. 70 > 70 is false, so it runs the ELSE and displays denied, not approved. The boundary value 70 is exactly where > and >= differ.
Incorrect. 70 is between 60 and 100 inclusive, so both parts of the AND are true and it DOES display approved.
Incorrect. s < 70 is false (70 is not less than 70), and NOT false is true, so this DOES display approved.
Question 5 of 6Spot the bug
A program should display discount when the amount in c is at least 100, and full price otherwise. The segment below is wrong for exactly one value of c. For which value does it produce the wrong output?
if c > 100: print("discount") else: print("full price")
Incorrect. 99 is below 100, so "full price" is correct here. The bug hides only at the boundary.
Correct. "At least 100" means 100 should get the discount, but c > 100 is false at exactly 100, so it wrongly prints "full price." The fix is c >= 100.
Incorrect. 101 > 100 is true, so it correctly prints "discount." The off-by-one error only shows at 100.
Incorrect. 150 > 100 is true, so "discount" is correct. Only the boundary value 100 is handled wrong.
Question 6 of 6Boolean logic
Which single condition is true for exactly the same values of t as the condition tested below?
if not (t >= 5 and t <= 9): print("outside")
Incorrect. No number is both less than 5 AND greater than 9, so this is never true. NOT of an AND becomes an OR, not an AND.
Correct. By De Morgan, NOT (t >= 5 AND t <= 9) becomes (t < 5) OR (t > 9). Each comparison flips and AND becomes OR.
Incorrect. The boundaries are wrong. Flipping t >= 5 gives t < 5 (strict), and flipping t <= 9 gives t > 9 (strict). Using <= and >= would wrongly include 5 and 9.
Incorrect. t > 5 OR t < 9 is true for every number, so it does not match. Watch both the operator flips and the AND becoming OR.
🎮 Lesson Game
Branch Predictor
Read each conditional and predict what it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • which branch runs?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • One-way IF
A store gives free shipping when the cart total is at least 50. total is set for you. Write one IF statement that prints free shipping when total is at least 50. Target output: free shipping
Level 2 • IF / ELSE
Problem 2 of 8 • Two-way selection
A login check: the correct code is 4321. entered is set for you. Print unlocked if entered equals the correct code, otherwise print locked. Remember Python equality is ==. Target output: locked
Level 3 • MOD
Problem 3 of 8 • Even or odd
A number n is set for you. Print even if n is divisible by 2, otherwise print odd. Use the modulo operator %. Target output: odd
Level 4 • Fix the bug
Problem 4 of 8 • Boundary error
This should print pass when score is at least 60 and fail otherwise. It has a boundary bug: at score = 60 it prints the wrong thing. Fix it so 60 prints pass. Target output: pass
Level 5 • Logical OR
Problem 5 of 8 • Combined condition
A ticket is half price for kids under 13 or seniors 65 and older. age is set for you. Print half price if the age qualifies, otherwise full price. Target output: half price
Level 6 • Create Task style
Problem 6 of 8 • Meaningful selection
This is the kind of selection you would identify in your Create Task: both branches are meaningful and reachable. temp holds a temperature reading. Print alert when temp is below 32 or above 100 (an unsafe reading), otherwise print normal. Target output: alert
Level 7 • Challenge
Problem 7 of 8 • you write the whole program
Open ended. Write the entire program yourself. A fitness tracker reports on one day of activity. Print one line for each check, in this order:
  • steps at least 10000: step goal met, otherwise keep walking
  • active minutes at least 30: active goal met, otherwise move more
  • steps divisible by 2: even steps, otherwise odd steps
Only the two inputs are given. Print exactly three lines. Target output (three lines): step goal met, then active goal met, then even steps
Level 8 • Challenge
Problem 8 of 8 • multi-step, every operator
Open ended. Write the entire program yourself. An online checkout prints one line for each rule, in this order:
  • total at least 75 OR the shopper is a member: free shipping, otherwise add shipping
  • member AND total at least 100: member discount, otherwise no discount
  • NOT a member: guest checkout, otherwise member checkout
Only the two inputs are given. Print exactly three lines. Target output (three lines): free shipping, then member discount, then member checkout

Frequently Asked Questions

A one-way selection is a single IF with no ELSE. The block runs only when the condition is true, and nothing happens when it is false. A two-way selection is IF/ELSE, where exactly one of the two blocks always runs: the IF block when the condition is true, the ELSE block when it is false.
In Python a single = means assignment (store a value into a variable) and a double == means "are these equal?" A condition needs a true or false result, so it must use ==. Writing if x = 5: is a syntax error and the program will not run. AP pseudocode uses a single = for equality, so switching to == is a habit you build when you start writing real Python.
With IF/ELSE the two blocks are mutually exclusive, so exactly one runs. With two separate IF statements, each condition is checked independently, so zero, one, or both blocks can run. This matters most when the first block changes a variable that the second condition tests, because the second IF then sees the updated value and may behave in a way IF/ELSE never could.
Use >= when the boundary value itself should count, and > when it should not. "At least 60" or "60 and up" includes 60, so use >= 60. "More than 60" excludes 60, so use > 60. Boundary errors fail for exactly one input value, which is why the exam loves to test them. Always ask yourself what should happen at the exact boundary.
Your Create Task program must use selection, and your written response has to identify the conditional, state its condition, describe both branches, and explain how it contributes to the program's purpose. Both branches must be meaningful and reachable for the selection to demonstrate the skill. Practicing conditionals here is directly practicing a scored part of the task.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable Topic 3.6 slide deck with animated branch traces, a boundary-and-Boolean bug bank, a print-ready IF/ELSE worksheet, and a unit quiz. View what's included →

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]