Week 10 · Unit 4: Variables in programming · 3 lessons
1
Choices with variables
Computing 6: Unit 4, pp. 52–67
GoalUse comparisons with variables and answers in if-then-else blocks and predict which branch runs.
New words
condition · a question in an if block that is either true or falsetrue and false · the only two answers that a condition can givecomparison · a test such as =, > or <, which compares two valuesattempts · a variable that counts how many tries someone has made
Explanation
A decision in Scratch uses an if block with a condition. The condition is a comparison, such as (answer) = [moon] or (score) > (10). A comparison is either true or false. If it is true, the blocks inside the if part run. If it is false, the blocks inside the else part run. Comparisons can use a variable, the answer block or a plain number or word. Scratch ignores the difference between capital and small letters, so MOON and moon are equal. A variable can also keep count of what the user does. Here the variable attempts goes up by 1 each time the user answers.
Worked examples
Try
User types
attempts
answer = moon?
The sprite says
1
sun
1
false
Wrong word. Try again.
2
moon
2
true
The door opens!
Trace the program after the flag is clicked. The user clicks the sprite and types sun, and then clicks again and types moon.
A comparison can use a number. The condition (score) > (10) is true when the score is 11 or more. It is false when the score is exactly 10, because 10 is not more than 10.
Be careful with the signs. The sign = tests whether two values are equal, and > and < test which one is bigger. In a set block the word to puts a value into a variable. It does not test anything.
Class activity
Door game (no computer): an adult chooses a secret word and writes it on a card, hidden under a cup. You are the sprite. Say ‘What is the secret word?’ and let a friend guess. Keep an attempts counter with pebbles in a bowl. Add one pebble for every guess. If the guess matches the card, say ‘The door opens!’. If not, say ‘Wrong word. Try again.’ How many pebbles did it take? Then change the rule so that the door also closes for good after 3 wrong tries. If you have a computer or tablet, build the program in Scratch.
Practice
1
What does the sprite say each time? The user types Tiger, then lion.
For Tiger it says Yes!, because Scratch ignores the difference between capital and small letters. For lion it says Not that one.
2
The variable score holds 10. Is the condition true or false? What about when score holds 11?
False when score is 10, because 10 is not more than 10. True when score is 11.
3
Write the blocks for this: if the age is less than 12, the sprite says Child ticket. Otherwise it says Adult ticket.
if <(age) < (12)> then, say [Child ticket] for (2) seconds, else, say [Adult ticket] for (2) seconds.
4
Nodira writes if <(score) = (10)> but she wants the message to appear when the score is 10 or more. What should she change?
She should use the sign > and the number 9, as in (score) > (9), or use two conditions joined with or. The equals sign is true only when the score is exactly 10.