Topic 3.5: Boolean Expressions | AP CSP Big Idea 3 | APCSExamPrep.com
Boolean Expressions
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
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.
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.
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 TrueisFalse, andnot FalseisTrue. -
AND (
and) isTrueonly when both sides areTrue. If either side isFalse, the whole thing isFalse. -
OR (
or) isTruewhen at least one side isTrue. It isFalseonly when both sides areFalse.
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.
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?
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:
- Resolve every relational comparison into a Boolean first.
- Apply NOT next (it binds tightest of the three logical operators).
- Apply AND.
- 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 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.
print(not (4 > 2))
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 |
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:
In your written response, name the expression and explain what makes it True or False:
- "The Boolean expression
score >= 100 AND NOT out_of_livesevaluates 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 >= 100and the negationNOT out_of_liveseach 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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
s is equal to the value in t, without causing an error. Which line does this correctly?True only when n is at least 10 and at most 20. It uses the wrong operator. For n = 100, what happens?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
x holds 4. Which expression does NOT evaluate to True?x is set. Print the result of comparing whether x is greater than 5. Target output: True
x is equal to 10. Use the equality operator, not assignment. Target output: True
x is greater than 5 and y is less than 3. Target output: True
x equals 5, but it uses a single =, which is a syntax error. Fix it so it prints the comparison. Target output: True
True when temp is not greater than 30, and False otherwise. Use not with a comparison. Target output: True
score is at least 70 or they have extra_credit. Print the single Boolean result. Target output: True
-
ageis at least 18 - they hold a ticket (
has_ticket) - they are not banned (
not banned)
age, has_ticket, and banned into one Boolean expression and print its value. Only the three inputs are given. Target output: True
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)
True
True
Frequently Asked Questions
x > 5 is a Boolean expression because the computer resolves it immediately to True or False.= 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.True or False and False evaluates the AND first (giving False) and then the OR, resulting in True. Parentheses override this order.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.🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]