AP Computer Science Principles · Study guide

Boolean Expressions

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Boolean Expressions

How comparisons produce true or false, how NOT, AND, and OR combine those values, and how to write and check conditions for ranges.

1True or false

A Boolean value is either true or false, nothing else. Programs make every decision with Boolean values: whether to show a discount, whether a password matches, whether a loop should stop. A Boolean expression is any expression that works out to one of these two values.

2The six comparisons

The relational operators compare two variables, expressions, or values, and the comparison evaluates to a Boolean.

OperatorMeansa = 7, b = 10a = 10, b = 10
a = bequalfalsetrue
a ≠ bnot equaltruefalse
a > bgreater thanfalsefalse
a < bless thantruefalse
a ≥ bgreater than or equal tofalsetrue
a ≤ bless than or equal totruetrue

In the reference language, = only tests whether two values are equal. Storing a value uses the arrow ←. Comparisons work on strings too: name = "Ana" is true only if every character matches.

A comparison’s result is a value you can store, which can make code easier to read:

age ← 16
isAdult ← age ≥ 18
DISPLAY(isAdult)
DISPLAY(NOT isAdult)

This displays false and then true.

3Words into comparisons

Most mistakes happen at the boundary, the one number where two operators disagree. Translate the words carefully, then test the boundary value.

WordsExpressionage = 18age = 19
at least 18age ≥ 18truetrue
more than 18age > 18falsetrue
at most 18age ≤ 18truefalse
under 18age < 18falsefalse
is 18age = 18truefalse
is not 18age ≠ 18falsetrue

age ≥ 18 and age > 18 agree for every age except 18 itself.

4NOT, AND, OR

The logical operators take Boolean values in and give one Boolean out.

  • NOT condition is true if condition is false, and false if it is true. It flips the value.
  • condition1 AND condition2 is true only if both are true.
  • condition1 OR condition2 is true if either one or both are true. It is false only when both are false.
ABA AND BA OR BNOT A
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue

Each operand of a logical operator must itself be a Boolean expression or a single Boolean value. To say “x is 5 or 6”, write x = 5 OR x = 6. Writing x = 5 OR 6 is not a valid condition, because 6 on its own is a number, not true or false.

age ← 70
isStudent ← false
discount ← age < 13 OR age ≥ 65 OR isStudent
DISPLAY(discount)

A ticket is discounted for a child, a senior, or a student. With an age of 70 this displays true; with 30 and no student card, false.

5Evaluating step by step

Work from the inside out: replace each comparison with its Boolean, apply each NOT, then combine with AND and OR. Parentheses tell you what goes together; use them whenever you mix operators.

Figure 1. Replace each comparison with its Boolean, apply NOT, then combine. Either side of OR being true would have been enough.

6Ranges and negations

Figure 2. “Between 1 and 10” needs AND. Swapping in OR gives an expression that is true for every number.
MeaningCorrectCommon wrong version
x is between 1 and 10, inclusivex ≥ 1 AND x ≤ 10x ≥ 1 OR x ≤ 10 (true for every number)
x is outside 1 to 10x < 1 OR x > 10x < 1 AND x > 10 (never true)
not both A and BNOT (A AND B), same as NOT A OR NOT BNOT A AND B (the NOT applies to A only)
neither A nor BNOT (A OR B), same as NOT A AND NOT BNOT A OR NOT B (that means “not both”)

The last two rows show a pattern: when you move a NOT inside parentheses, it flips every condition and swaps AND with OR. Checking all four combinations of true and false confirms each pair gives the same result.

The key idea

Comparisons give true or false. AND needs both sides true, OR needs at least one, and NOT flips. Write a range with AND, an outside-the-range test with OR, and test every condition at its boundary value.

7Worked examples

Worked Example 1 Evaluate

Equal values

With a ← 4 and b ← 4, evaluate a = b, a ≠ b, a ≥ b, a > b.

  1. 1
    a = b: true.
  2. 2
    a ≠ b: false.
  3. 3
    a ≥ b: true.
  4. 4
    a > b: false.
Answer: true, false, true, false. Only the operators that include equality are true when the values match.
Worked Example 2 Evaluate

Where the parentheses go

Evaluate NOT (4 > 2) AND (3 ≤ 3) and NOT ((4 > 2) AND (3 ≤ 3)).

  1. 1
    Both comparisons are true: 4 > 2 and 3 ≤ 3.
  2. 2
    First expression: NOT true is false, and false AND true is false.
  3. 3
    Second expression: true AND true is true, and NOT true is false.
  4. 4
    Both are false here, but they are not the same expression. Change AND to OR in the first and it becomes false OR true, which is true.
Answer: false and false.
Worked Example 3 Write and test

Too cold or too hot

Write a condition that is true when temp is below 0 or above 35, and test it.

  1. 1
    “Below 0” is temp < 0, “above 35” is temp > 35, and either one is enough, so use OR: temp < 0 OR temp > 35.
  2. 2
    Test the boundaries and one value on each side:
    tempresult
    -4true
    0false
    20false
    35false
    36true
Answer: temp < 0 OR temp > 35
Worked Example 4 Choose the expression

A valid score

A score is valid if it is from 60 to 100, inclusive. Which is right: score ≥ 60 OR score ≤ 100 or score ≥ 60 AND score ≤ 100?

  1. 1
    Try a value that should be rejected, like 101. The OR version gives true OR false, which is true: wrong.
  2. 2
    The AND version gives true AND false, which is false: right.
  3. 3
    Check the edges with the AND version: 59 is false, 60 is true, 100 is true, 101 is false.
Answer: score ≥ 60 AND score ≤ 100

8Common mistakes

Common Mistake 1 · Reading OR as “one or the other, not both”

OR is true when both sides are true as well.

Common Mistake 2 · Using OR for a range

x ≥ 1 OR x ≤ 10 is true for every number. A range between two values needs AND.

Common Mistake 3 · Leaving out half a comparison

x = 5 OR 6 is not a condition. Each side of AND or OR needs its own comparison: x = 5 OR x = 6.

Common Mistake 4 · Skipping the boundary

≥ and > differ at one value. Always test that value.

Explain a Boolean result like this

“With a = 3, b = 7, and c = 5, a > b is false, so NOT (a > b) is true. c = 5 is also true. An OR is true when at least one side is true, so the expression is true.”

Give the Boolean for each comparison, then apply NOT, AND, and OR in order.