AQA GCSE Computer Science (8525), section 3.2.4 (Relational operations in a programming language)
What they do
A relational operator compares two values and gives a Boolean result: True or False. They are used in the conditions of selection and iteration.
The six operators
| Meaning | Pseudo-code | Python | Example | Result |
|---|---|---|---|---|
| equal to | = |
== |
5 = 5 |
True |
| not equal to | ≠ |
!= |
5 ≠ 3 |
True |
| less than | < |
< |
3 < 3 |
False |
| greater than | > |
> |
4 > 3 |
True |
| less than or equal to | ≤ |
<= |
3 ≤ 3 |
True |
| greater than or equal to | ≥ |
>= |
2 ≥ 3 |
False |
Different languages use different symbols. Always check what the question uses.
In programs
age = 16
print(age >= 16) # True
print(age == 18) # False
print(age != 16) # False
IF age ≥ 18 THEN
OUTPUT "Adult"
ENDIF
Equal to and assignment
In many languages, = assigns a value and == compares. In AQA pseudo-code, assignment uses ←, and = is used for comparison.
Comparing strings
Strings can be compared too. "apple" = "apple" is true, and comparing with < and > uses alphabetical (character code) order.
Boundary values
Pay close attention to < and <=. For "an adult is 18 or over" you need age >= 18; using age > 18 would wrongly leave out 18-year-olds.
Common mistake: mixing up < and ≤ (or > and ≥). Check whether the boundary value itself should count. Also, do not use a single = for comparing in Python: that is assignment.
Exam tip: test your condition with the boundary value and the values either side of it. For "at least 10" use ≥ 10; for "more than 10" use > 10.
Check yourself
What data type is the result of comparing two values?
Boolean (True or False).
Is 7 ≥ 7 true or false?
True.
Which condition means "at least 18"?
age ≥ 18 (age >= 18).