GoalWrite conditions with comparison operators and decide whether they are true or false.
New words
condition · a test that can only be true or falsecomparison operator · a symbol that compares two values: =, !=, <, >, <= or >=selection · a place in an algorithm where one set of steps or another is chosen, depending on a conditionBoolean value · one of only two values: true or false
Explanation
Selection means the program chooses between steps. It chooses by testing a condition. A condition compares two values with a comparison operator, and the answer is always true or false. In pseudocode the six operators are =, !=, <, >, <= and >=. Here a single = means ‘is equal to’, because assignment is written with the arrow. Words must be turned into the right operator. ‘At least 8’ includes 8, so it is >= 8. ‘More than 8’ does not include 8, so it is > 8. In a flowchart, the condition is written in a diamond, and one arrow leaves it for yes and one for no.
Operator
Means
Example
Answer
=
is equal to
6 = 6
true
!=
is not equal to
6 != 6
false
<
is less than
4 < 9
true
>
is greater than
4 > 9
false
<=
is less than or equal to
5 <= 5
true
>=
is greater than or equal to
4 >= 5
false
Worked examples
In words
Condition
the stall has more than 5 apples
apples > 5
the stall has at least 5 apples
apples >= 5
the bag holds exactly 5 apples
apples = 5
the bag does not hold 5 apples
apples != 5
there are no more than 5 apples
apples <= 5
Everyday words become conditions. Look for the words that tell you whether the number itself counts.
A bike light flowchart tests one condition. With 18 typed, hour >= 18 is true, so Lights on is shown. With 17 typed it is false, so Lights off is shown.
Condition
Values put in
Answer
apples > 5
6 > 5
true
apples = 5
6 = 5
false
price != 4
4 != 4
false
price <= 4
4 <= 4
true
name = "ali"
"Ali" = "ali"
false
Conditions can use variables. Suppose apples holds 6, price holds 4 and name holds the string Ali. Work out each answer by replacing the names with the values. Strings are compared letter for letter, so Ali and ali are not equal.
Class activity
Human true or false (no computer). Make six number cards from paper (for example 3, 7, 10, 12, 15 and 20) and four condition cards: n < 10, n >= 12, n != 7, n = 15. Turn over one number card and one condition card. Replace n with the number and call out true or false. Your partner checks. Score a point for each right answer. Then write two condition cards of your own, for example n > 5 and n <= 10, and play again. If you have a computer or tablet, ask an adult to show you how Python prints True or False for a comparison.
Practice
1
Write a condition for each sentence. (a) The child is under 13 (variable age). (b) The basket has at least 8 eggs (variable eggs). (c) The card is not red (variable colour). (d) The player has scored exactly 3 (variable goals).
(a) age < 13. (b) eggs >= 8. (c) colour != "red". (d) goals = 3. ‘At least’ includes the number, so it uses >=.
2
Here a is 7, b is 7 and c is 12. Is each condition true or false?
Condition
True or false?
a = b
?
c < a
?
c - a >= 5
?
a != b + 1
?
a = b is true (7 = 7). c < a is false (12 < 7). c - a >= 5 is true, because 12 - 7 is 5 and 5 >= 5. a != b + 1 is true, because b + 1 is 8 and 7 is not 8.
3
Trace the flowchart with the inputs 15, 9 and 22. What is shown each time?
15: 15 < 15 is false, so No coat needed. 9: 9 < 15 is true, so Wear a coat. 22: 22 < 15 is false, so No coat needed.
4
A pupil writes players < 6 for ‘no more than 6 players’. Test the condition with 6 players and say what is wrong. Write the correct condition.
With 6 players the condition players < 6 is false, but 6 is allowed. ‘No more than’ includes 6, so the correct condition is players <= 6.