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.
| 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.
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.
| 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.
4NOT, AND, OR
The logical operators take Boolean values in and give one Boolean out.
NOT conditionis true ifconditionis false, and false if it is true. It flips the value.condition1 AND condition2is true only if both are true.condition1 OR condition2is 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.
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.
6Ranges and negations
| 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.
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
Equal values
With a ← 4 and b ← 4, evaluate a = b, a ≠ b, a ≥ b, a > b.
- 1
a = b: true. - 2
a ≠ b: false. - 3
a ≥ b: true. - 4
a > b: false.
Where the parentheses go
Evaluate NOT (4 > 2) AND (3 ≤ 3) and NOT ((4 > 2) AND (3 ≤ 3)).
- 1Both comparisons are true:
4 > 2and3 ≤ 3. - 2First expression:
NOT trueis false, andfalse AND trueis false. - 3Second expression:
true AND trueis true, andNOT trueis false. - 4Both 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.
Too cold or too hot
Write a condition that is true when temp is below 0 or above 35, and test it.
- 1“Below 0” is
temp < 0, “above 35” istemp > 35, and either one is enough, so use OR:temp < 0 OR temp > 35. - 2Test the boundaries and one value on each side:
temp result -4 true 0 false 20 false 35 false 36 true
temp < 0 OR temp > 35A 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?
- 1Try a value that should be rejected, like 101. The OR version gives
true OR false, which is true: wrong. - 2The AND version gives
true AND false, which is false: right. - 3Check the edges with the AND version: 59 is false, 60 is true, 100 is true, 101 is false.
score ≥ 60 AND score ≤ 1008Common mistakes
OR is true when both sides are true as well.
x ≥ 1 OR x ≤ 10 is true for every number. A range between two values needs AND.
x = 5 OR 6 is not a condition. Each side of AND or OR needs its own comparison: x = 5 OR x = 6.
≥ and > differ at one value. Always test that value.
“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.