AP Computer Science Principles · Unit 3 lecture · Part 4 of 14 · 8 min

3.5 Boolean expressionsTrue, false, AND, OR, NOT

Mr. Thompkins
Every decision a program makes comes down to one question with two possible answers. Is the password right? Is the score at least 70? Is the robot blocked? Those yes-or-no answers have a name: Booleans.

A Boolean value is either true or false. Relational operators compare two values and produce a Boolean: =, ≠, >, <, ≥ and ≤. Note that the exam uses a single = to test whether two values are equal, and the arrow ← for assignment, so they never get confused.

OperatorRead it asWith x ← 7Value
a = bequalsx = 7true
a ≠ bis not equal tox ≠ 7false
a > bgreater thanx > 7false
a < bless thanx < 10true
a ≥ bat leastx ≥ 7true
a ≤ bat mostx ≤ 6false
The six relational operators, each tested with x ← 7.

Logical operators combine Booleans. NOT flips a Boolean: NOT true is false. AND is true only when both sides are true. OR is true when at least one side is true, including when both are. A truth table lists every combination.

ABA AND BA OR BNOT A
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue
The truth table. AND needs both; OR needs at least one; NOT flips.
?Wait, is OR "one or the other but not both"?
Not in programming. A OR B is true when A is true, when B is true, or when both are. It's false only when both are false. Everyday English sometimes means "but not both"; the exam never does.

Ranges, and how to flip a condition

The most common Boolean on the exam describes a range. "t is from 60 to 80, inclusive" means t is at least 60 AND t is at most 80: (t ≥ 60) AND (t ≤ 80). The outside of that range is "t is below 60 OR above 80": (t < 60) OR (t > 80). A single number can't be below 60 and above 80 at once, so the outside must use OR.

6080(t ≥ 60) AND (t ≤ 80)6080(t < 60) OR (t > 80)inside: AND. outside: OR.
Inside a range: AND, with ≥ and ≤ for inclusive ends. Outside: OR, with the opposite comparisons.

To negate a combined condition, flip each comparison and swap AND with OR. NOT ((x > 10) AND (y = 2)) is the same as (x ≤ 10) OR (y ≠ 2). Notice the opposite of > is ≤, not <: if x isn't greater than 10, it could be equal to 10.

?Wait, what's the opposite of each comparison?
Opposite of > is ≤. Opposite of < is ≥. Opposite of = is ≠. The opposite always includes the "equal" case that the original left out, or leaves out the one it included.
Pause and think
Is (age ≥ 13) OR (age ≤ 19) a good test for "age is a teenager"?
I thought about it. Show me
No. Every age makes it true: 5 is ≤ 19 and 40 is ≥ 13. A range needs AND: (age ≥ 13) AND (age ≤ 19).
Evaluate the Booleans
Let age ← 16 and permit ← true. Evaluate each: (a) (age ≥ 16) AND permit (b) NOT ((age < 18) OR permit) (c) (age > 16) OR (NOT permit).
  1. (a) age ≥ 16 is 16 ≥ 16: true. permit is true. true AND true is true.
    Why?
    ≥ includes equality, so 16 ≥ 16 is true.
  2. (b) Inside the parentheses: age < 18 is true, so the OR is true (one true side is enough). NOT true is false.
    Why?
    Evaluate the inside first, then apply NOT.
  3. (c) age > 16 is 16 > 16: false. NOT permit is NOT true: false. false OR false is false.

Answer: (a) true (b) false (c) false

Stuck? Get a hint
  1. Replace each comparison with true or false first.
  2. Then combine with AND or OR, using the truth table.
  3. NOT goes last, on the value inside its parentheses.
Check yourself
Which expression is equivalent to NOT ((score > 70) AND (late = false))?
  1. A(score > 70) OR (late = true)
  2. B(score ≤ 70) OR (late = true)
  3. C(score ≤ 70) AND (late = true)
  4. D(score < 70) OR (late = false)
Show the answer

B. Flip each comparison (> becomes ≤, late = false becomes late = true) and swap AND with OR. Choice D uses < instead of ≤, which would leave out a score of exactly 70.

Check yourself
Which expression is true exactly when temp is outside the range 32 to 50, inclusive?
  1. A(temp < 32) AND (temp > 50)
  2. B(temp ≤ 32) OR (temp ≥ 50)
  3. C(temp < 32) OR (temp > 50)
  4. DNOT (temp > 32)
Show the answer

C. Outside means below 32 or above 50: (temp < 32) OR (temp > 50). With AND nothing is ever true, and ≤ and ≥ would wrongly include 32 and 50, which are inside the range.

Exam tip from Mr. T

When a question says "inclusive", the endpoints count: use ≥ and ≤. Then test your expression on three numbers: one inside, one outside, and one endpoint. It takes ten seconds and catches most mistakes.

  • A Boolean is true or false. Relational operators: =, ≠, >, <, ≥, ≤.
  • AND: both true. OR: at least one true. NOT: flips.
  • Inside a range: AND. Outside a range: OR.
  • To negate: flip each comparison (> ↔ ≤, < ↔ ≥, = ↔ ≠) and swap AND with OR.