Relational Operators
To evaluate a comparison, replace each variable with the value it holds, work out any arithmetic, and only then apply the comparison operator. The answer is a single Boolean value: either True or False.
Imagine a bakery bakes 5 trays with 8 buns on each tray, and needs at least 43 buns. To evaluate trays * 8 >= 43:
5 * 8 >= 4340 >= 43The arithmetic always comes first, because the comparison needs two finished values: the total number of buns and the target of 43. Comparing first would mean multiplying the True or False answer by 5, which the expression never asks you to do.
weeks = 5
days = weeks * 7
print(days >= 30)
print(days != 35)Output:
True
False
| Comparison | After substituting and working out | Result |
|---|---|---|
4 * 6 > 20 | 24 > 20 | True |
c - 9 >= 12 when c is 21 | 12 >= 12 | True |
18 // 4 == 5 | 4 == 5 | False |
d * 2 != 30 when d is 15 | 30 != 30 | False |
In every row the middle step leaves one operator sitting between two values, such as 24 and 20, and that final question is what produces the True or False. The result is that single Boolean value, not the numbers you compared.
OCR GCSE Computer Science · Relational Operators