GoalUse the equal-to operator to compare two values and say whether the result is true or false.
New words
comparison operator · a symbol that compares two values and gives true or falseequal to · the operator = ; it is true when the two values are the samevariable · a named place that stores a value that can changerandom · chosen by chance, so it may be different each time
Explanation
A comparison operator compares two values and gives true or false. The equal-to operator is written =. It gives true when the value on the left is the same as the value on the right. Otherwise it gives false. We can compare numbers, words and variables. Other comparison operators are not equal to, greater than and less than. In an if … then … else block, the condition is often an equal-to test, such as ‘score = 5’. A ‘pick random’ block chooses a whole number between two limits, so the same program can give a different result each time.
Test
True or false?
6 = 6
true
6 = 7
false
cat = cat
true
cat = dog
false
Worked examples
Test
Value of lives
Result
lives = 3
3
true
lives = 0
3
false
lives = 4
3
false
A variable lives holds 3. Each test compares the value in lives with a number.
A door game picks a random number from 1 to 3. The Prize is behind door 2. The script says ‘Prize!’ only when the number equals 2. The other two numbers give ‘Try again’.
Block
What it does
Result
set [score v] to (5)
puts 5 into score
score is now 5
(score) = (5)
asks if score is 5
true or false
Do not mix up ‘set’ and ‘equals’. One puts a value into a variable, and the other asks a question.
Class activity
Equal or not (pairs). Make number cards 1 to 6 and have a die. Player A says a test such as ‘die = 4’ and Player B rolls. Player A says true or false. Score 1 point for each correct answer. Then put a few counters in a cup labelled ‘lives’ and test things like ‘lives = 2’. Change the counters and test again. Which test is true most often?