A Boolean expression evaluates to one of two values, true or false. Relational operators such as > and ≠ compare two values to make a Boolean, and the logical operators NOT, AND and OR combine Booleans: AND is true only when both sides are true, and OR is true when at least one side is. In AP® Computer Science Principles, you evaluate a condition by substituting the values, working out each comparison, and then combining the results.
Quick check
After x ← 7 and then x ← x - 2, what value does x hold?Show answerHide answer
It holds 5. The right side uses the old value: 7 − 2 = 5.
Is the statement "8 is greater than 3, and 2 is greater than 5" true or false?Show answerHide answer
False. The first part is true, but the second is false, and "and" needs both.
Know these already? Jump to the practice questions
What you'll be able to do
- State the value of a comparison that uses
=,≠,>,<,≥or≤. - Use the truth tables for
NOT,ANDandOR. - Evaluate a compound condition for given values of its variables.
- Decide whether two Boolean expressions are equivalent.
Key terms
Boolean value
A value that is either true or false.
- In plain English
- The answer to a yes-or-no question.
- Don't write
- "A Boolean value can be 0, 1 or 2."
Relational operator
An operator that compares two values and gives a Boolean value: =, ≠, >, <, ≥ or ≤.
- In plain English
- A test such as "is bigger than" or "is equal to".
- Don't write
- "
x = 5stores 5 inx." Storing uses←.
Logical operator
NOT, AND or OR: an operator that works on Boolean values and gives a Boolean value.
- In plain English
- The words that flip or join yes-or-no answers.
- Don't write
- "
ORis false when both sides are true."
Equivalent expressions
Two Boolean expressions that give the same value for every possible input.
- In plain English
- Different wording, same result every time.
- Don't write
- "They are equivalent because they agree for one value."
How do relational operators make a Boolean?
On the exam reference sheet: a = b, a ≠ b, a > b, a < b, a ≥ b and a ≤ b, each of which evaluates to true or false.
With x holding 7:
| Expression | Value | Because |
|---|---|---|
x > 5 | true | 7 is more than 5 |
x ≠ 7 | false | 7 equals 7 |
x ≥ 7 | true | "or equal to" includes 7 |
x < 7 | false | 7 is not less than itself |
In a condition, = asks a question ("are these equal?") and changes nothing. Only ← stores a value. A Boolean can be stored like any other value: passed ← score ≥ 50 puts true or false into passed.
Check yourself
Show answerHide answer
False. 50 is not less than 50; score ≤ 50 would be true.
How do NOT, AND and OR work?
On the exam reference sheet: NOT condition, condition1 AND condition2 and condition1 OR condition2.
| p | q | NOT p | p AND q | p OR q |
|---|---|---|---|---|
| true | true | false | true | true |
| true | false | false | false | true |
| false | true | true | false | true |
| false | false | true | false | false |
NOTflips a Boolean.ANDis true only in the first row, where both sides are true.ORis false only in the last row, where both sides are false.
Key idea
AND needs both sides true; OR needs at least one side true, and both is fine.
Check yourself
Show answerHide answer
true AND false is false, and true OR false is true.
How do you evaluate a compound condition?
A compound condition joins several comparisons. Evaluate it in this order:
- Substitute the current value of each variable.
- Work out each comparison to true or false.
- Apply each
NOTto the part it is attached to. - Combine with
ANDandOR, innermost parentheses first.
Novark always puts parentheses around each part, so the order is never in doubt. With a holding 4 and b holding 9, (a > 3) AND (b < 5) becomes true AND false, which is false. Changing AND to OR makes it true.
Check yourself
Show answerHide answer
True: true OR false is true.
How can you tell if two expressions are equivalent?
Two expressions are equivalent only if they agree for every input. One matching case proves nothing; one disagreeing case proves they are not equivalent.
- For Boolean variables, check all rows of a truth table: two variables give four rows.
- For numbers, test the boundary values, such as 4, 5 and 6 for a comparison with 5.
Learn this: two patterns that come from the truth tables.
| Expression | Equivalent to |
|---|---|
NOT (x > 5) | x ≤ 5 |
NOT (p AND q) | (NOT p) OR (NOT q) |
NOT (p OR q) | (NOT p) AND (NOT q) |
The last two are De Morgan's laws: moving NOT inside the parentheses flips AND to OR, and OR to AND.
Check yourself
Show answerHide answer
No. When x is 10, NOT (x < 10) is true but x > 10 is false. It is equivalent to x ≥ 10.
Worked example 1: Evaluate a compound condition
Question: temp holds 68 and raining holds false. Is this condition true or false?
((temp ≥ 60) AND (NOT raining)) OR (temp > 85)
What tells you the method
Several parts joined by AND and OR: evaluate each part, then combine from the inside out.
Step 1: Evaluate each part.
Part Value temp ≥ 60true NOT rainingtrue (temp ≥ 60) AND (NOT raining)true temp > 85false WhyNOTflips false to true, andANDwith two true sides is true.Step 2: Combine with
OR.true OR falseis true.WhyORneeds only one true side.
Check it
If raining were true, the AND part would be false and so would the whole condition, so the answer depends on rain, as the code intends.
Answer
The condition is true.
Worked example 2: Design a condition and test its edges
Question: A cinema gives a reduced price to customers under 12 or aged 65 and over. Design a condition that stores true in reduced for exactly those ages. What value does reduced hold when age is 65?
What tells you the method
Two separate age ranges qualify, and either is enough, so the parts join with OR.
Step 1: State the input, output and cases.
Input:
age. Output: true or false. Cases:age < 12andage ≥ 65.Why"Under 12" excludes 12; "65 and over" includes 65.
Step 2: Write the condition.
reduced ← (age < 12) OR (age ≥ 65)WhyA customer needs to meet only one case, so
ORfits.Step 3: Test both sides of each boundary.
Ages 11, 12, 64 and 65 give true, false, false and true.
WhyMistakes in
<versus≤show up only at the boundary.
Check it
With AND instead, no age could be under 12 and 65 or over at once, so reduced would always be false.
Answer
It holds true.
Worked example 3: Choose the equivalent expression
Question: Which expression is equivalent to NOT ((x > 3) AND (y = 5))?
- A)
(x ≤ 3) AND (y ≠ 5) - B)
(x < 3) OR (y ≠ 5) - C)
(x ≤ 3) OR (y ≠ 5) - D)
(x > 3) OR (y = 5)
What tells you the method
NOT in front of parentheses: apply De Morgan's laws, then negate each comparison.
Step 1: Move
NOTinside.(NOT (x > 3)) OR (NOT (y = 5)).WhyMoving
NOTinside flipsANDtoOR.Step 2: Negate each comparison.
NOT (x > 3)isx ≤ 3, andNOT (y = 5)isy ≠ 5.WhyThe opposite of "more than 3" includes 3 itself.
Check it
Try x = 3, y = 5. The original is NOT (false AND true), which is true. C gives true OR false, which is true; B gives false OR false, which is false.
Answer
C
Your turn
Question: x holds 4 and y holds 9. Is (NOT (x > 5)) AND ((y < 3) OR (y = 10)) true or false?
Step 1: Evaluate the left side.
x > 5is false, soNOT (x > 5)is true.Step 2: Your go: evaluate the right side, then combine with
AND.
Show the full solutionHide the solution
Step 2. y < 3 is false and y = 10 is false, so the OR is false. true AND false is false.
Answer: The condition is false.
Traps
Trap
OR is false when both sides are true.
It seems right because everyday "or" often means one or the other. But OR is true when at least one side is true, so true OR true is true.
What is the value of (5 > 2) OR (7 > 2)?
True: both sides are true, and OR needs at least one.
Trap
NOT (x > 5) is the same as x < 5.
It seems right because the opposite of "greater" sounds like "less". But when x is 5, x > 5 is false, so NOT (x > 5) is true, while x < 5 is false. The correct match is x ≤ 5.
Which is equivalent to NOT (x > 5): x < 5 or x ≤ 5?
x ≤ 5, because it includes 5.
Trap
NOT (p AND q) is the same as (NOT p) AND (NOT q).
It seems right because NOT looks like it can be shared out over both parts. But when p is true and q is false, the first is true and the second is false. The AND must flip to OR.
Which is equivalent to NOT (p AND q): (NOT p) AND (NOT q), or (NOT p) OR (NOT q)?
(NOT p) OR (NOT q).
What's examined
On the AP® Computer Science Principles exam, Boolean expressions belong to Big Idea 3: Algorithms and Programming. Multiple-choice questions ask for the value of a condition, the condition that matches a description, or an equivalent expression. The relational and logical operators are on the exam reference sheet; De Morgan's laws and negating comparisons are Learn this.
Practice questions
- Recall
pholds true andqholds false. Which expression is true?
- A)
p AND q - B)
NOT p - C)
NOT (p OR q) - D)
p OR q
Show answer
Answer: D. true OR false is true, because one true side is enough.
Why the others are wrong:
- A needs both sides true, but
qis false. - B flips true to false.
- C is
NOT true, which is false.
- Apply
xholds 7 andyholds 2. Is(x > 5) AND (y > 5)true or false?
Show answer
Answer: It is false.
Marking points:
x > 5is true, buty > 5is false.ANDneeds both, so the result is false.
- Apply
xholds 7 andyholds 2. Is(NOT (x = 7)) OR (y < 3)true or false?
Show answer
Answer: It is true.
Marking points:
x = 7is true, soNOT (x = 7)is false.y < 3is true, andfalse OR trueis true.
- Exam-style Which condition is true exactly when
nis from 10 to 20, including 10 and 20?
- A)
(n ≥ 10) AND (n ≤ 20) - B)
(n ≥ 10) OR (n ≤ 20) - C)
(n > 10) AND (n < 20) - D)
(n ≤ 10) AND (n ≥ 20)
Show answer
Answer: A. Both limits must hold at once, and ≥ and ≤ include the end values.
Why the others are wrong:
- B uses
OR, so every number passes: 50 is at least 10. - C leaves out 10 and 20.
- D reverses the limits, so no number passes.
- Exam-style Which expression is equivalent to
NOT (done OR late)?
- A)
(NOT done) OR (NOT late) - B)
(NOT done) AND (NOT late) - C)
(NOT done) OR late - D)
(NOT done) AND late
Show answer
Answer: B. Moving NOT inside flips OR to AND and negates both parts.
Why the others are wrong:
- A negates both parts but keeps
OR. - C negates only
doneand keepsOR. - D negates only
done.
- Exam-style
pandqare Boolean variables. For how many of the four combinations of their values is(p AND (NOT q)) OR ((NOT p) AND q)true?
Show answer
Answer: 2
Marking points:
- It is true when exactly one of
pandqis true: true/false and false/true. - Both true or both false makes each
ANDpart false.
- Exam-style
goOutshould be true only when it is not raining and the temperature is from 50 to 90. Give the number of the line that stops the code working.
1: temp ← 72
2: raining ← false
3: tooHot ← (temp > 90)
4: tooCold ← (temp < 50)
5: niceTemp ← (NOT tooHot) AND (NOT tooCold)
6: goOut ← (NOT raining) OR niceTemp
Show answer
Answer: Line 6.
Marking points:
- Both conditions must hold, so line 6 needs
AND. - With
OR, a rainy day at 72 gives true:false OR true.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- A comparison gives true or false;
=compares and←stores. NOTflips,ANDneeds both, andORneeds at least one.- Substitute, evaluate the comparisons, apply
NOT, then combine. - The opposite of
x > 5isx ≤ 5, notx < 5. - Pushing
NOTinside parentheses swapsANDandOR.