Relational and Boolean operations
Decisions in programs depend on Boolean expressions — expressions that evaluate to true or false. These are built from relational operators (compare two values) and logical operators (combine Boolean values).
Relational operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| = | Equal to | 5 = 5 | true |
| != | Not equal | 5 != 6 | true |
| < | Less than | 4 < 5 | true |
| <= | Less than or equal | 5 <= 5 | true |
| > | Greater than | 6 > 5 | true |
| >= | Greater than or equal | 6 >= 5 | true |
These work on integers, reals, characters and strings (alphabetical order). For strings, "apple" < "banana" is true (a comes before b).
Logical (Boolean) operators
| Operator | Meaning | Truth table |
|---|---|---|
| AND | Both inputs true | T AND T = T; T AND F = F; F AND F = F |
| OR | At least one input true | T OR T = T; T OR F = T; F OR F = F |
| NOT | Inverts the input | NOT T = F; NOT F = T |
These often appear in compound conditions:
IF age >= 18 AND hasLicense = true THEN
OUTPUT "Can drive"
ENDIF
The whole expression is true only when both sub-expressions are true.
IF day = "Saturday" OR day = "Sunday" THEN
OUTPUT "Weekend!"
ENDIF
The expression is true if either side is true.
IF NOT loggedIn THEN
OUTPUT "Please sign in"
ENDIF
NOT inverts a Boolean.
Operator precedence in Booleans
The usual order is: NOT first, then AND, then OR. Use brackets to make intent clear:
IF (a > 0 AND b > 0) OR c = 0 THEN ...
Without brackets, AND binds tighter than OR. a OR b AND c means a OR (b AND c).
Common Boolean idioms
- Range check:
x >= 0 AND x <= 100 - Outside range:
x < 0 OR x > 100 - Either-or, not both (XOR):
(a OR b) AND NOT (a AND b)— XOR isn't built into AQA pseudocode but can be expressed. - Contains check (string): depends on language; often a function like
POSITION(s, c) > 0.
De Morgan's laws (HT but useful)
- NOT (A AND B) = (NOT A) OR (NOT B)
- NOT (A OR B) = (NOT A) AND (NOT B)
These help simplify conditions and avoid double negatives. E.g. "neither raining nor sunny" = NOT (raining OR sunny) = NOT raining AND NOT sunny.
✦Worked example— Worked examples
Example 1. Evaluate (5 > 3) AND (10 = 10).
- 5 > 3 → true
- 10 = 10 → true
- true AND true → true
Example 2. Evaluate NOT (4 < 2 OR 6 > 1).
- 4 < 2 → false
- 6 > 1 → true
- false OR true → true
- NOT true → false
Example 3. Decide whether to give a discount: customer must be over 65 OR have a loyalty card.
IF age > 65 OR hasLoyaltyCard = true THEN apply discount
⚠Common mistakes— Pitfalls
- Using AND instead of OR (or vice versa). "x is between 1 and 10" → AND. "x is 1 or x is 2" → OR.
- Comparing a string to an integer → always false in many languages.
- Overusing brackets to the point of confusion — group related conditions clearly.
- Forgetting that NOT has highest precedence:
NOT a OR bmeans(NOT a) OR b. - Confusing assignment (
SET x TO 5) with comparison (x = 5).
Truth table practice
Evaluate P AND (Q OR NOT R) for P=T, Q=F, R=T:
- NOT R → F
- Q OR F → F
- P AND F → F
➜Try this— Quick check
Build a Boolean condition for "user is allowed to enter": age must be at least 12, AND either they have a ticket OR they are with a paying adult.
age >= 12 AND (hasTicket = true OR withPayingAdult = true)
AI-generated · claude-opus-4-7 · v3-deep-computer-science