Topic 3.6: Conditionals | AP CSP Big Idea 3 | APCSExamPrep.com
Conditionals
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
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.
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.
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") }
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.
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.
t is set below. Type the exact word this prints (lowercase).t = 6 if t < 3 or t > 6: print("out") else: print("in")
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.
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:
- Write down the current value of every variable the condition uses.
- Substitute those values into the Boolean expression and evaluate it to true or false. Evaluate
NOTfirst, thenAND, thenOR. - 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.
- Update your variable table with any changes the block made.
- Continue to the next statement. If it is another IF, start over at step 1 with the updated values.
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.
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 |
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:
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 displayskeep 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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
if m = n: uses a single = (assignment) where a condition needs the == equality operator, so Python raises a syntax error before any output.m = n inside the condition is assignment, not comparison, and Python rejects it as a syntax error.m = n is assignment, so Python raises a SyntaxError and nothing prints. The fix is if m == n:.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
s has been assigned the value 70. Which segment does NOT display approved?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?t as the condition tested below?total is set for you. Write one IF statement that prints free shipping when total is at least 50. Target output: free shipping
entered is set for you. Print unlocked if entered equals the correct code, otherwise print locked. Remember Python equality is ==. Target output: locked
n is set for you. Print even if n is divisible by 2, otherwise print odd. Use the modulo operator %. Target output: odd
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
age is set for you. Print half price if the age qualifies, otherwise full price. Target output: half price
temp holds a temperature reading. Print alert when temp is below 32 or above 100 (an unsafe reading), otherwise print normal. Target output: alert
- steps at least 10000:
step goal met, otherwisekeep walking - active minutes at least 30:
active goal met, otherwisemove more - steps divisible by 2:
even steps, otherwiseodd steps
- total at least 75 OR the shopper is a member:
free shipping, otherwiseadd shipping - member AND total at least 100:
member discount, otherwiseno discount -
NOT a member:
guest checkout, otherwisemember checkout
Frequently Asked Questions
= 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.>= 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.🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]