Week 24 · Unit 10: Be a game developer · 3 lessons
2
Three kinds of loop
Computing 4: Unit 10, pp. 138–151
GoalTurn an algorithm with a repeat step into a Scratch program, and choose between forever, repeat and repeat until.
New words
indefinite loop · a loop that repeats without stopping, such as forevercount-controlled loop · a loop that repeats a set number of timescondition-controlled loop · a loop that repeats until something becomes true
Explanation
There are three kinds of loop. A forever loop is an indefinite loop: it never stops by itself. A repeat loop is count-controlled: it stops after a number of times that we choose. A repeat until loop is condition-controlled: it stops when a condition becomes true. In an algorithm, a step such as “repeat steps 4 to 6 five more times” means the steps are done once already and then five more times. So the Scratch repeat block says 6, which is the number of times in all. Steps before the repeat are done only once, so they stay outside the loop.
Kind of loop
Scratch block
When it stops
indefinite
forever
never, until the stop sign is clicked
count-controlled
repeat (10)
after the set number of times
condition-controlled
repeat until <>
when the condition becomes true
Worked examples
Step
Instruction
1
Start program when the green flag is clicked
2
Go to x: −150, y: 0
3
Point in direction 90
4
Move 20 steps
5
Switch to the next costume
6
Wait 0.5 seconds
7
Repeat steps 4 to 6 five more times
A bee algorithm. Steps 2 and 3 happen once. Steps 4 to 6 are done once and then five more times, which makes 6 times in all.
The Scratch program. Steps 1 to 3 stay above the loop. Steps 4 to 6 go inside repeat (6). Trace it: each round moves the bee 20 steps and takes 0.5 seconds. Six rounds move it 6 × 20 = 120 steps, from x = −150 to x = −30, and take 6 × 0.5 = 3 seconds.
Round
x at the start
x at the end
Is x > −40 afterwards?
1
−150
−130
no
2
−130
−110
no
3
−110
−90
no
4
−90
−70
no
5
−70
−50
no
6
−50
−30
yes, stop
The same job with a repeat until <(x position) > (-40)> loop. Scratch checks the condition before each round. At the start of round 6 the bee is at x = −50, which is not more than −40, so round 6 runs and x becomes −30. Now −30 is more than −40, so the loop stops. It ran 6 times, just like repeat (6).
Class activity
Three loops game (with family or friends): one person is the caller and the others do the actions. The caller says “Repeat 4 times: clap and stamp”. Everyone does it exactly four times. The caller says “Forever: wave”. Everyone waves until the caller shouts “stop sign!”. The caller says “Repeat until you hear a whistle: hop”. Everyone hops until the whistle. Which of the three was count-controlled? Which one needed someone to watch a condition?
Practice
1
Write this algorithm as a Scratch program. Then work out how far the turtle moves in all and how long the program takes.
Step
Instruction
1
Start program when the green flag is clicked
2
Change the colour effect by 20
3
Change x by 15
4
Wait 0.3 seconds
5
Repeat steps 2 to 4 five more times
when green flag clicked, then repeat (6) containing change color effect by 20, change x by 15 and wait 0.3 seconds. The turtle moves 6 × 15 = 90 steps to the right. The program takes 6 × 0.3 = 1.8 seconds.
2
How many times does the sprite say “Hi!”, and how long does the program take?
4 times. Each round takes 1 + 1 = 2 seconds, so 4 rounds take 8 seconds.
3
The algorithm says “Repeat steps 3 and 4 two more times”. How many rounds does this program do? How many should it do? Fix it and give the final size.
It does 2 rounds, but the algorithm needs 3 (once and two more times). Change repeat (2) to repeat (3). The size goes from 50 to 50 + 3 × 10 = 80 %.
4
Which loop would you use? A: music that plays until the player presses a key. B: a firework that sparkles 5 times. C: a car that keeps driving until it touches a wall.
A: repeat until a key is pressed (or forever, if the music never needs to stop). B: repeat (5). C: repeat until the car is touching the wall.