GoalChoose the right comparison operator for a condition, and say whether a comparison is true or false.
New words
comparison operator · a symbol that compares two values, such as < or ==true / false · the only two possible answers to a comparison (a Boolean value)boundary value · a test value that is exactly on the edge of a condition
Explanation
A condition compares two values with a comparison operator. The answer is always true or false. There are six operators: ==, !=, <, >, <= and >=. A single = stores a value in a variable, but a double == asks whether two values are equal. The operators with an equals sign include the value itself, so 6 <= 6 is true but 6 < 6 is false. Words help you choose: ‘at least 12’ means >= 12, and ‘fewer than 5’ means < 5. Always test the boundary value, because that is where most mistakes hide.
In Python, print can show the answer of a comparison. Here age is 12. Notice that 12 >= 12 is true but 12 > 12 is false, so the boundary value matters.
In words
Condition
at least 12
age >= 12
fewer than 5
count < 5
exactly 20
answer == 20
not 0
number != 0
no more than 30
pupils <= 30
more than 100
score > 100
Turn words into operators. Always ask yourself: is the number itself included?
In a flowchart the condition goes in the diamond. This flowchart checks whether any seats are left on a minibus. It uses == to compare the number with zero.
Class activity
True or false (no computer): write the numbers 1 to 12 on slips and put them in a bag. Write the six operators on six more cards. Pick two number slips and one operator card and lay them out, for example 7, >=, 4. Your partner calls ‘true’ or ‘false’ out loud. Score one point for each correct call. Then pick one number slip and a card, and write your own condition, such as ‘is the number at least 9?’. Test it with every slip, and write down which numbers are true. If you have a computer or tablet, type some comparisons into the Python shell with an adult.
Practice
1
Say true or false for each comparison.
Comparison
True or false?
8 > 3
?
5 <= 5
?
7 != 7
?
4 == 9
?
12 < 12
?
8 > 3 is True. 5 <= 5 is True. 7 != 7 is False. 4 == 9 is False. 12 < 12 is False, because 12 is not less than itself.
Three lines: True, False, True. score >= 15 is true because 15 equals 15. score < 15 is false. score != 20 is true because 15 is not 20.
3
Write the comparison for each: (a) a ticket is free for ages 4 and under, (b) a team has more than 10 players, (c) the answer is exactly 50, (d) the temperature is not 0.
(a) age <= 4. (b) players > 10. (c) answer == 50. (d) temperature != 0.
4
This flowchart should output ‘Pass’ for marks of 40 or more. What goes wrong when the mark is exactly 40? How do you fix it?
A mark of 40 is not more than 40, so it follows the no arrow and the output is ‘Try again’. Fix: change the condition to mark >= 40?.