Topic 3.5: Boolean Expressions | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.5 Boolean Expressions
3.5
Big Idea 3 • Algorithms & Programming

Boolean Expressions

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Truth Tracer game 💻 Live Python editor AAP-2.E • AAP-2.F

After this lesson, you will be able to:

  • Recognize that a comparison is itself a value that evaluates to True or False
  • Use the six relational operators and match AP pseudocode symbols to their Python spellings
  • Apply NOT, AND, and OR, knowing AND needs both sides and OR needs at least one
  • Evaluate compound Boolean expressions step by step in the order NOT, then AND, then OR
  • Rewrite an expression using De Morgan's law to flip AND and OR
  • Write a precise Boolean condition to drive the logic of your Create Performance Task
📈 Exam weight: Boolean expressions underpin every conditional and loop in Big Idea 3, and they show up directly in evaluate-the-expression and spot-the-bug items. The == versus = swap and the AND versus OR swap are two of the most common traps on the whole exam.
💡 Think about this first

Every decision a program makes comes down to one question with only two possible answers: True or False. Is the score high enough? Is the user logged in? Is the input empty? Before a program can choose a path, it must reduce a messy real-world question to a single Boolean value. This lesson is about building and evaluating those expressions with total confidence, one comparison and one operator at a time.

What a Boolean Expression Is

A Boolean expression is any expression that evaluates to exactly one of two values: True or False. That is the entire universe of answers a Boolean can have. The simplest way to produce one is a relational operator, which compares two values and reports whether the comparison holds. The comparison itself is not a question the computer leaves open; it collapses immediately to a single Boolean value.

This idea that a comparison is a value trips up many students. When you write x > 5, Python does not store the words "x greater than 5." It computes a result, True or False, and that result can be printed, stored, or combined just like a number. The AP reference sheet lists six relational operators, shown here next to their Python spellings.

AP Pseudocode Python Meaning
a = b a == b equal to
a ≠ b a != b not equal to
a > b a > b greater than
a < b a < b less than
a ≥ b a >= b greater than or equal to
a ≤ b a <= b less than or equal to
x = 7
print(x > 5)
print(x <= 7)
print(x == 5)
print(x != 5)
x <- 7
DISPLAY(x > 5)
DISPLAY(x <= 7)
DISPLAY(x = 5)
DISPLAY(NOT (x = 5))

Read each line as a claim that is then judged true or false. With x holding 7: x > 5 is True, x <= 7 is True (7 is equal, which counts), x == 5 is False, and x != 5 is True. Notice that Python prints these with a capital letter: True and False, never lowercase.

⚠ == is not =

In Python a single = means "assign this value," while a double == means "compare for equality." Writing x = 5 where you meant x == 5 does not compare anything, and inside print(x = 5) it is a hard error. In AP pseudocode a single = is the comparison, so the exam deliberately tests whether you can switch between the two notations.

✍ Mini Exercise 1 • Predict the value
Both sides are compared, then combined with and. What does this display?
print(5 > 3 and 2 > 4)

The Three Logical Operators: NOT, AND, OR

Relational operators give you one Boolean at a time. Logical operators let you combine or flip Booleans to build richer tests. There are exactly three, and each has a simple rule you can memorize.

  • NOT (not) reverses a Boolean: not True is False, and not False is True.
  • AND (and) is True only when both sides are True. If either side is False, the whole thing is False.
  • OR (or) is True when at least one side is True. It is False only when both sides are False.
a = True
b = False
print(not a)
print(a and b)
print(a or b)
a <- TRUE
b <- FALSE
DISPLAY(NOT a)
DISPLAY(a AND b)
DISPLAY(a OR b)

With a as True and b as False: not a is False, a and b is False (b breaks the "both" requirement), and a or b is True (a alone satisfies "at least one"). The single most useful intuition to carry into the exam: OR is far easier to satisfy than AND. AND demands everything; OR is happy with anything.

🎯 What the exam expects

The CED (AAP-2.F) expects you to evaluate expressions using NOT, AND, and OR and to know their truth tables cold. A favorite trap swaps AND for OR: an expression that looks like it demands two things but really only needs one, or the reverse. Always ask: does this test require both, or just one?

✍ Mini Exercise 2 • Why is it True?
With x set to 3, this displays True. Which reason is correct?
x = 3
print(x > 5 or x < 10)

Evaluating Compound Expressions Step by Step

When an expression mixes operators, order matters. Evaluate in this fixed order, exactly like arithmetic precedence:

  1. Resolve every relational comparison into a Boolean first.
  2. Apply NOT next (it binds tightest of the three logical operators).
  3. Apply AND.
  4. Apply OR last (it binds loosest).

Take True or False and False. AND goes before OR, so first False and False becomes False, leaving True or False, which is True. If you had wrongly worked left to right you would have gotten False. Parentheses override this order, and using them generously is a smart habit.

x = 8
y = 2
# evaluate step by step
print(x > 5 and y > 5)
print(x > 5 or y > 5)
x <- 8
y <- 2
# evaluate step by step
DISPLAY(x > 5 AND y > 5)
DISPLAY(x > 5 OR y > 5)

Here x is 8 and y is 2. For the first line, x > 5 is True and y > 5 is False, so True and False is False. For the second line, True or False is True. Same two comparisons, different combiner, opposite result. That contrast is the heart of AND versus OR.

⚠ NOT binds tighter than you think

not x > 10 is read as not (x > 10), because the comparison resolves first and then NOT flips it. Students sometimes imagine NOT reaching across an AND or OR; it does not. It flips only the single Boolean immediately to its right unless parentheses say otherwise.

✍ Mini Exercise 3 • Type the value
Type the exact value this displays (capitalized, as Python prints it).
print(not (4 > 2))
prints:

De Morgan: Flipping AND and OR

One equivalence appears again and again on the exam. When you push a NOT inside a parenthesized AND or OR, the operator flips and each piece gets its own NOT. This is De Morgan's law:

  • not (a and b) is the same as (not a) or (not b)
  • not (a or b) is the same as (not a) and (not b)

The intuition: "it is not the case that both are true" means "at least one is false," which is an OR of the negations. Distributing the NOT turns AND into OR and OR into AND. Test it against every combination and the two sides always agree.

a = True
b = False
# De Morgan: the two lines always match
print(not (a and b))
print((not a) or (not b))
a <- TRUE
b <- FALSE
# De Morgan: the two lines always match
DISPLAY(NOT (a AND b))
DISPLAY((NOT a) OR (NOT b))

With a as True and b as False: the left side not (a and b) is not (False), which is True. The right side (not a) or (not b) is False or True, which is True. They match, and they would match for all four combinations of a and b. Recognizing this pair lets you rewrite an awkward condition into a cleaner one.

Key Vocabulary

Term Definition Example
Boolean expression An expression that evaluates to True or False x > 5
Relational operator Compares two values and yields a Boolean ==, !=, >, <, >=, <=
NOT Reverses a Boolean value not True is False
AND True only when both sides are true a and b
OR True when at least one side is true a or b
De Morgan's law not (a and b) equals (not a) or (not b) flips AND to OR
📋 Create Performance Task • Boolean expressions drive your logic

Every meaningful decision your Create Task program makes rests on a Boolean expression. The rubric wants selection and iteration that respond to input, and the condition inside them is always a Boolean. Writing that condition precisely, and being able to explain it, is exactly what earns the point.

A point-earning example

Suppose your program decides whether a player has cleared a level. The rule is: they clear it when their score is at least 100 and they did not run out of lives. That single rule is one Boolean expression:

cleared <- (score >= 100) AND (NOT out_of_lives)

In your written response, name the expression and explain what makes it True or False:

  • "The Boolean expression score >= 100 AND NOT out_of_lives evaluates to True only when both parts hold."
  • "Because it uses AND, a player who scores 100 but has run out of lives still evaluates to False."
  • "The comparison score >= 100 and the negation NOT out_of_lives each produce a Boolean, and AND combines them into the single decision value."

The trap to avoid

Do not swap AND for OR. With OR, a player who scored zero would still clear the level as long as they had lives left, which is not your rule. Say out loud whether your rule needs both parts or just one, then pick the matching operator. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: resolve each comparison, then combine with and.
What does this code segment display?
x = 8 y = 2 print(x > 5 and y > 5)
Incorrect. x > 5 is True, but y > 5 is False, and AND needs both sides True. The result is False.
Correct. x > 5 is True and y > 5 is False, so True and False is False. AND demands both.
Incorrect. This is valid Python that prints a Boolean, not an error.
Incorrect. print always displays its value here; it displays False.
Question 2 of 6Spot the bug
Predict first: which operator actually compares for equality?
A programmer wants to display whether the value in s is equal to the value in t, without causing an error. Which line does this correctly?
# choose the correct line
Incorrect. A single = means assignment, and inside print(...) that is a syntax error, not a comparison.
Correct. The double == compares for equality and yields a Boolean, so it displays True or False.
Incorrect. =< is not a valid operator. The less-than-or-equal operator is <=, and it does not test equality anyway.
Incorrect. A single ! is not an operator in Python. Not-equal is written !=, and equality is ==.
Question 3 of 6Spot the bug
This expression is meant to display True only when n is at least 10 and at most 20. It uses the wrong operator. For n = 100, what happens?
n = 100 print(n >= 10 or n <= 20)
Incorrect. n >= 10 is True, and OR only needs one True side, so it displays True, not False.
Correct. 100 >= 10 is True, so OR short-circuits to True, but 100 is not in 10 to 20. The bug is using or where the rule needs and.
Incorrect. It does display True, but for n = 100 that is wrong; 100 is outside the range 10 to 20.
Incorrect. The line is valid Python and prints a Boolean.
Question 4 of 6I, II, III
Let p and q be Boolean values. Which of the following are always equal to not (p and q) for every combination of p and q?
  • I. (not p) or (not q)
  • II. (not p) and (not q)
  • III. not p or not q
Incorrect. I is correct by De Morgan, but III is also equal because not p or not q groups as (not p) or (not q).
Incorrect. II is (not p) and (not q), which equals not (p or q), a different expression.
Correct. De Morgan gives not (p and q) = (not p) or (not q), so I holds. III is the same thing since NOT binds before OR. II is not (p or q), so it differs.
Incorrect. II equals not (p or q). Try p True, q False: not (p and q) is True, but II is False.
Question 5 of 6NOT question
The variable x holds 4. Which expression does NOT evaluate to True?
Incorrect. 4 < 10 is True, so this does evaluate to True.
Incorrect. 4 > 10 is False, and not False is True, so this evaluates to True.
Incorrect. 4 > 2 is True and 4 < 6 is True, so the AND is True.
Correct. 4 > 5 is False and 4 < 1 is False, so False or False is False. This is the one that is not True.
Question 6 of 6Boolean logic
Predict first: apply and before or.
What does this code segment display?
p = True q = False r = False print(p or q and r)
Correct. AND binds before OR, so q and r is False and False, which is False. Then p or False is True.
Incorrect. Working left to right gives False, but that ignores precedence. AND is evaluated before OR, giving True.
Incorrect. This is valid Python and prints a Boolean.
Incorrect. print displays the value; it displays True.
🎮 Lesson Game
Truth Tracer
Evaluate each Boolean expression and predict whether it displays True or False. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • True or False?
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 • a single comparison
x is set. Print the result of comparing whether x is greater than 5. Target output: True
Level 2 • Equality
Problem 2 of 8 • == not =
Print the result of checking whether x is equal to 10. Use the equality operator, not assignment. Target output: True
Level 3 • AND
Problem 3 of 8 • both must hold
Print the result of a compound test: x is greater than 5 and y is less than 3. Target output: True
Level 4 • Fix the bug
Problem 4 of 8 • = vs ==
This line is supposed to display whether x equals 5, but it uses a single =, which is a syntax error. Fix it so it prints the comparison. Target output: True
Level 5 • NOT
Problem 5 of 8 • flip a comparison
Print True when temp is not greater than 30, and False otherwise. Use not with a comparison. Target output: True
Level 6 • Create Task style
Problem 6 of 8 • a meaningful condition
A player passes when their score is at least 70 or they have extra_credit. Print the single Boolean result. Target output: True
Level 7 • Challenge
Problem 7 of 8 • you write the whole expression
Open ended. Write the entire program yourself. A visitor may enter only when all of these are true:
  • age is at least 18
  • they hold a ticket (has_ticket)
  • they are not banned (not banned)
Combine age, has_ticket, and banned into one Boolean expression and print its value. Only the three inputs are given. Target output: True
Level 8 • Challenge
Problem 8 of 8 • prove a De Morgan pair
Open ended. Write the entire program yourself. Given Boolean a and b, print two lines to show De Morgan's law makes them match:
  • first line: not (a and b)
  • second line: (not a) or (not b)
Target output (two lines):
True
True

Frequently Asked Questions

A Boolean expression is any expression that evaluates to exactly one of two values, True or False. A comparison such as x > 5 is a Boolean expression because the computer resolves it immediately to True or False.
A single = assigns a value to a variable, while a double == compares two values for equality and produces a Boolean. In AP pseudocode a single = is the comparison, so you switch notations between the two.
and is True only when both sides are True. or is True when at least one side is True, and False only when both sides are False. OR is much easier to satisfy than AND.
Comparisons resolve first, then NOT, then AND, then OR. So True or False and False evaluates the AND first (giving False) and then the OR, resulting in True. Parentheses override this order.
De Morgan's law says not (a and b) equals (not a) or (not b), and not (a or b) equals (not a) and (not b). Pushing a NOT inside parentheses flips AND to OR and negates each part.
📦
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.5 slide deck with animated truth tables, a NOT, AND, and OR trap bank, a De Morgan practice 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]