AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Flowcharts that choose

Week 4 · Unit 7.1: Block it out · 3 lessons
2

Comparing values

Computing 7: Unit 7.1, pp. 8–43
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.

OperatorMeansExampleTrue or false?
==equal to5 == 5True
!=not equal to5 != 5False
<less than6 < 6False
>greater than9 > 4True
<=less than or equal to6 <= 6True
>=greater than or equal to10 >= 12False
Worked examples
Python
1age = 12
2print(age >= 12)
3print(age > 12)
4print(age == 12)
5print(age != 12)
Output
True
False
True
False
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 wordsCondition
at least 12age >= 12
fewer than 5count < 5
exactly 20answer == 20
not 0number != 0
no more than 30pupils <= 30
more than 100score > 100
Turn words into operators. Always ask yourself: is the number itself included?
StartINPUT seats_leftseats_left == 0?yesnoOUTPUT "Bus is full"OUTPUT "Climb aboard"End
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.
ComparisonTrue or false?
8 > 3?
5 <= 5?
7 != 7?
4 == 9?
12 < 12?
2
What does this program show?
Python
1score = 15
2print(score >= 15)
3print(score < 15)
4print(score != 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.
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?
StartINPUT markmark > 40?yesnoOUTPUT "Pass"OUTPUT "Try again"End