Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.5

Boolean Expressions: Study Guide (Computer Science Principles 3.5)

How relational and logical operators build and evaluate Boolean expressions, with truth tables, boundary values, range tests and De Morgan equivalences. Two worked examples and misconceptions.

Every decision a program makes comes down to a question with a yes-or-no answer: is the password correct, is the score high enough, has the timer reached zero. In programming the answer is a Boolean value, either true or false. This topic covers how to build and evaluate Boolean expressions with the relational operators (=, ≠, >, <, ≥, ≤) and the logical operators (NOT, AND, OR). Conditionals (Topic 3.6) and loops (Topic 3.8) are driven by these expressions, so accuracy here pays off for the rest of the unit.

Notation. The exam uses its own pseudocode, not a real programming language. The code in this pack is Marlbridge’s rendering of that reference notation, not a copy of the official sheet: ← is assignment, = tests equality, lists start at index 1, DISPLAY shows a value, and braces mark blocks. Use the Exam Reference Sheet from your teacher for the official wording.

Boolean values and relational operators

A Boolean value is either true or false; there is nothing in between. A relational operator compares two values and produces a Boolean:

Operator Meaning Example Value
a = b equal 4 = 4 true
a ≠ b not equal 4 ≠ 4 false
a > b greater than 7 > 3 true
a < b less than 7 < 3 false
a ≥ b greater than or equal 5 ≥ 5 true
a ≤ b less than or equal 6 ≤ 5 false

Two cautions. First, = is a test, not an assignment (Topic 3.1). Second, a comparison is evaluated before it is used: in x ← (5 > 3), the variable x receives the Boolean true.

Logical operators

Logical operators combine Boolean values. Each operand is a Boolean value or a Boolean expression.

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

Read the table as follows.

  • NOT p flips the value.
  • p AND q is true only when both are true.
  • p OR q is true when at least one is true, including when both are. It is inclusive.

Precedence. Many languages evaluate NOT first, then AND, then OR. Do not rely on memory for this: use brackets in your own code so the meaning is obvious, and follow the brackets in exam questions.

Number line for the expression age at least 13 AND age at most 19 A number line from 10 to 22. A thick line with filled circles at both ends runs from 13 to 19. Ages in that range make the expression true; ages to the left of 13 or to the right of 19 make it false. 10111213141516171819202122age ≥ 13 AND age ≤ 19 is truefalsefalseFilled circles: the end values 13 and 19 are included
Figure 1. AND of two comparisons selects the overlap. Ages 13 to 19 (inclusive) are true; everything outside is false.

Worked example 1: evaluating compound expressions

Let a be 7 and b be 12. Evaluate (i) (a < b) AND NOT(b MOD 5 = 0) and (ii) (a > 10) OR (b = 12 AND a ≠ 7).

a ← 7
b ← 12
DISPLAY((a < b) AND NOT(b MOD 5 = 0))
DISPLAY((a > 10) OR (b = 12 AND a ≠ 7))

Output: true false

(i) Step by step.

Part Working Value
a < b 7 < 12 true
b MOD 5 12 MOD 5 = 2 2
b MOD 5 = 0 2 = 0 false
NOT(...) NOT false true
whole expression true AND true true

(ii) Step by step.

Part Working Value
a > 10 7 > 10 false
b = 12 12 = 12 true
a ≠ 7 7 ≠ 7 false
b = 12 AND a ≠ 7 true AND false false
whole expression false OR false false

Check. Evaluate the smallest parts first and work outwards. A single slip, such as reading a ≠ 7 as true, flips the answer, so always write the value of each small comparison.

Worked example 2: from English to Boolean expressions

Task A: “age is between 13 and 19, inclusive”. The expression is age ≥ 13 AND age ≤ 19. Both conditions must hold, so AND is right. A common wrong version uses OR: age ≥ 13 OR age ≤ 19 is true for every age (a 5-year-old satisfies the second part, a 40-year-old the first), so it filters nothing. Test the correct version on four ages:

DISPLAY(5 ≥ 13 AND 5 ≤ 19)
DISPLAY(13 ≥ 13 AND 13 ≤ 19)
DISPLAY(19 ≥ 13 AND 19 ≤ 19)
DISPLAY(40 ≥ 13 AND 40 ≤ 19)

Output: false true true false

The end values 13 and 19 are true, which confirms that ≥ and ≤ include the ends (Figure 1). Using > and < would wrongly exclude them.

Task B: “n is divisible by 3 or by 5, but not by both”. Write the two basic facts, then combine: (n MOD 3 = 0 OR n MOD 5 = 0) AND NOT(n MOD 3 = 0 AND n MOD 5 = 0). Test with 3, 5, 15 and 7:

n divisible by 3 divisible by 5 expression
3 true false true
5 false true true
15 true true false
7 false false false

Fifteen is the interesting case: it satisfies “3 or 5” but also “both”, so the NOT(... AND ...) part removes it.

Task C: equivalent expressions (De Morgan’s laws). Some Boolean expressions can be written in a different but equivalent form. The pair that matters most is

NOT(p AND q) is equivalent to (NOT p) OR (NOT q), and NOT(p OR q) is equivalent to (NOT p) AND (NOT q).

Prove one by truth table:

p q p AND q NOT(p AND q) NOT p NOT q (NOT p) OR (NOT q)
true true true false false false false
true false false true false true true
false true false true true false true
false false false true true true true

The two right-hand columns match in all four rows, so the expressions are equivalent. In words: “it is not true that both are open” means “at least one is closed”.

Common misconceptions

  • “OR means exactly one.” p OR q is also true when both are true.
  • “age ≥ 13 OR age ≤ 19 means between.” It is true for every age. A range needs AND.
  • “≥ and > are interchangeable.” 5 ≥ 5 is true; 5 > 5 is false. Boundary values are where marks are lost.
  • “x = 5 OR 6 means x is 5 or 6.” Each side of OR must be a Boolean expression: write x = 5 OR x = 6.
  • “NOT(a AND b) is NOT a AND NOT b.” The negation changes AND to OR: NOT(a AND b) equals (NOT a) OR (NOT b).
  • “= assigns.” In a Boolean expression = compares. Assignment is ←.

Where this leads

Boolean expressions are the conditions inside IF statements (Topic 3.6, then nested in Topic 3.7), the stopping tests of REPEAT UNTIL loops (Topic 3.8), and the building blocks of equivalent algorithms in Topic 3.9.

Next steps: try the practice questions, skim the revision notes the night before, and tick off the topic checklist. The next topic is Topic 3.6, Conditionals.

Quick questions

Is OR true when both sides are true?

Yes. OR is inclusive: p OR q is true when at least one of p and q is true, including when both are.

How do I write "between 13 and 19"?

Use AND with two comparisons: age ≥ 13 AND age ≤ 19. Use > and < only if the end values should be excluded.

What is the difference between = and ← in the exam notation?

The arrow ← assigns a value to a variable; the equals sign = compares two values and evaluates to true or false.

Are NOT(a AND b) and NOT a AND NOT b the same?

No. NOT(a AND b) is equivalent to (NOT a) OR (NOT b). Negating changes AND to OR.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.