AP · Computer Science Principles · Boolean Logic and Selection

Boolean Expressions

12 min readUpdated December 1, 2026FreeAP Computer Science Principles, Algorithms and Programming

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

  1. 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.

  2. 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, AND and OR.
  • 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 = 5 stores 5 in x." 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
"OR is 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:

ExpressionValueBecause
x > 5true7 is more than 5
x ≠ 7false7 equals 7
x ≥ 7true"or equal to" includes 7
x < 7false7 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

With score holding 50, what is the value of score < 50?
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.

pqNOT pp AND qp OR q
truetruefalsetruetrue
truefalsefalsefalsetrue
falsetruetruefalsetrue
falsefalsetruefalsefalse
  • NOT flips a Boolean.
  • AND is true only in the first row, where both sides are true.
  • OR is 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

What are true AND false, and true OR false?
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:

  1. Substitute the current value of each variable.
  2. Work out each comparison to true or false.
  3. Apply each NOT to the part it is attached to.
  4. Combine with AND and OR, 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

With a holding 4 and b holding 9, what is (a > 3) OR (b < 5)?
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.

ExpressionEquivalent 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

Is NOT (x < 10) equivalent to x > 10?
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.

  1. Step 1: Evaluate each part.

    PartValue
    temp ≥ 60true
    NOT rainingtrue
    (temp ≥ 60) AND (NOT raining)true
    temp > 85false
    Why

    NOT flips false to true, and AND with two true sides is true.

  2. Step 2: Combine with OR.

    true OR false is true.

    Why

    OR needs 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.

  1. Step 1: State the input, output and cases.

    Input: age. Output: true or false. Cases: age < 12 and age ≥ 65.

    Why

    "Under 12" excludes 12; "65 and over" includes 65.

  2. Step 2: Write the condition.

    reduced ← (age < 12) OR (age ≥ 65)
    
    Why

    A customer needs to meet only one case, so OR fits.

  3. Step 3: Test both sides of each boundary.

    Ages 11, 12, 64 and 65 give true, false, false and true.

    Why

    Mistakes 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.

  1. Step 1: Move NOT inside.

    (NOT (x > 3)) OR (NOT (y = 5)).

    Why

    Moving NOT inside flips AND to OR.

  2. Step 2: Negate each comparison.

    NOT (x > 3) is x ≤ 3, and NOT (y = 5) is y ≠ 5.

    Why

    The 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?

  1. Step 1: Evaluate the left side.

    x > 5 is false, so NOT (x > 5) is true.

  2. 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

  1. Recall p holds true and q holds 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 q is false.
  • B flips true to false.
  • C is NOT true, which is false.
  1. Apply x holds 7 and y holds 2. Is (x > 5) AND (y > 5) true or false?
Show answer

Answer: It is false.

Marking points:

  • x > 5 is true, but y > 5 is false.
  • AND needs both, so the result is false.
  1. Apply x holds 7 and y holds 2. Is (NOT (x = 7)) OR (y < 3) true or false?
Show answer

Answer: It is true.

Marking points:

  • x = 7 is true, so NOT (x = 7) is false.
  • y < 3 is true, and false OR true is true.
  1. Exam-style Which condition is true exactly when n is 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.
  1. 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 done and keeps OR.
  • D negates only done.
  1. Exam-style p and q are 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 p and q is true: true/false and false/true.
  • Both true or both false makes each AND part false.
  1. Exam-style goOut should 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.
  • NOT flips, AND needs both, and OR needs at least one.
  • Substitute, evaluate the comparisons, apply NOT, then combine.
  • The opposite of x > 5 is x ≤ 5, not x < 5.
  • Pushing NOT inside parentheses swaps AND and OR.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Algorithms and Programming

All Boolean Logic and Selection notes

Part of Novark's AP Computer Science Principles notes