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

If, then, else

Week 19 · Unit 8: Be an innovator · 3 lessons
2

Is it equal?

Computing 5: Unit 8, pp. 104–118
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.

TestTrue or false?
6 = 6true
6 = 7false
cat = cattrue
cat = dogfalse
Worked examples
TestValue of livesResult
lives = 33true
lives = 03false
lives = 43false
A variable lives holds 3. Each test compares the value in lives with a number.
Hostwhenclickedsetdoortopick random1to3sayPrize!for2secondssayTry againfor2secondsifdoor=2thenelse
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’.
BlockWhat it doesResult
set [score v] to (5)puts 5 into scorescore is now 5
(score) = (5)asks if score is 5true 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?

Practice
1
True or false? (a) 12 = 12, (b) 9 = 10, (c) 4 + 3 = 7, (d) dog = dig
2
The variable laps holds 5. Which of these tests are true?
setlapsto5laps=5laps=6laps=0
3
How many different numbers can spin be? Which of them give ‘Win!’?
Spinnerwhenclickedsetspintopick random1to4sayWin!for2secondssayNext timefor2secondsifspin=4thenelse
4
What is the difference between ‘set score to 3’ and ‘score = 3’?