AQA GCSE Computer Science (8525), section 3.2.5 (Boolean operations in a programming language)
The three operators
Boolean operators combine or change conditions (which are True or False).
| Operator | Meaning | True when |
|---|---|---|
| NOT | the opposite | the condition is false |
| AND | both conditions | both are true |
| OR | either condition | at least one is true |
Truth tables
| A | B | A AND B | A OR B |
|---|---|---|---|
| False | False | False | False |
| False | True | False | True |
| True | False | False | True |
| True | True | True | True |
| A | NOT A |
|---|---|
| False | True |
| True | False |
Combining conditions
IF age ≥ 13 AND age ≤ 19 THEN
OUTPUT "Teenager"
ENDIF
IF day = "Saturday" OR day = "Sunday" THEN
OUTPUT "Weekend"
ENDIF
IF NOT (password = "secret") THEN
OUTPUT "Wrong password"
ENDIF
Python
age = 15
day = "Friday"
print(age >= 13 and age <= 19) # True
print(day == "Saturday" or day == "Sunday") # False
print(not (age > 18)) # True
Order
NOT is worked out first, then AND, then OR. Use brackets to make the order clear.
Common patterns
- A range: use
AND, e.g.number >= 1 AND number <= 10. - Either of two values: use
OR, e.g.answer = "y" OR answer = "Y". - Outside a range:
number < 1 OR number > 10.
Common mistake: using AND where OR is needed for values outside a range. "A number that is not between 1 and 10" is n < 1 OR n > 10: no number can be both below 1 and above 10. Also, you cannot write x = 1 OR 2: each side of OR must be a full condition.
Exam tip: work out each small condition first (True or False), then combine them. Use a small truth table if a condition has two or three parts.
Check yourself
When is A OR B false?
Only when both A and B are false.
Write a condition that is true when x is between 5 and 10 inclusive.
x ≥ 5 AND x ≤ 10.
What is NOT (3 > 5)?
True.