---
title: "Boolean Expressions | FiveStudy"
description: "How the six comparisons produce true or false, how NOT, AND, and OR combine them, and how to write and test conditions for ranges."
url: https://fivestudy.com/guides/ap-csp-3-5-boolean-expressions
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Boolean Expressions

[Figure: FiveStudy]

FiveStudy

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

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.

1. True or false 2. The six comparisons 3. Words into comparisons 4. NOT, AND, OR 5. Evaluating step by step 6. Ranges and negations 7. Worked examples 8. Common mistakes

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

## 2 The six comparisons

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

| Operator | Means | a = 7, b = 10 | a = 10, b = 10 |
| --- | --- | --- | --- |
| `a = b` | equal | false | true |
| `a ≠ b` | not equal | true | false |
| `a > b` | greater than | false | false |
| `a < b` | less than | true | false |
| `a ≥ b` | greater than or equal to | false | true |
| `a ≤ b` | less than or equal to | true | true |

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

## 3 Words into comparisons

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

| Words | Expression | age = 18 | age = 19 |
| --- | --- | --- | --- |
| at least 18 | `age ≥ 18` | true | true |
| more than 18 | `age > 18` | false | true |
| at most 18 | `age ≤ 18` | true | false |
| under 18 | `age < 18` | false | false |
| is 18 | `age = 18` | true | false |
| is not 18 | `age ≠ 18` | false | true |

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

## 4 NOT, 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.

| A | B | A AND B | A OR B | NOT A |
| --- | --- | --- | --- | --- |
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |

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

## 5 Evaluating 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.

## 6 Ranges and negations

**Figure 2.** “Between 1 and 10” needs AND. Swapping in OR gives an expression that is true for every number.

| Meaning | Correct | Common wrong version |
| --- | --- | --- |
| x is between 1 and 10, inclusive | `x ≥ 1 AND x ≤ 10` | `x ≥ 1 OR x ≤ 10` (true for every number) |
| x is outside 1 to 10 | `x < 1 OR x > 10` | `x < 1 AND x > 10` (never true) |
| not both A and B | `NOT (A AND B)`, same as `NOT A OR NOT B` | `NOT A AND B` (the NOT applies to A only) |
| neither A nor B | `NOT (A OR B)`, same as `NOT A AND NOT B` | `NOT 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.

## 7 Worked 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:

   | temp | result |
   | --- | --- |
   | -4 | true |
   | 0 | false |
   | 20 | false |
   | 35 | false |
   | 36 | true |

**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`

## 8 Common 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.

FiveStudy AP® Computer Science Principles · Boolean Expressions
