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

Reusable blocks and loops

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 loopScratch blockWhen it stops
indefiniteforevernever, until the stop sign is clicked
count-controlledrepeat (10)after the set number of times
condition-controlledrepeat until <>when the condition becomes true
Worked examples
StepInstruction
1Start program when the green flag is clicked
2Go to x: −150, y: 0
3Point in direction 90
4Move 20 steps
5Switch to the next costume
6Wait 0.5 seconds
7Repeat 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.
whenclickedgo to x:-150y:0point in direction90move20stepsnext costumewait0.5secondsrepeat6
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.
Roundx at the startx at the endIs x > −40 afterwards?
1−150−130no
2−130−110no
3−110−90no
4−90−70no
5−70−50no
6−50−30yes, 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.
StepInstruction
1Start program when the green flag is clicked
2Change the colour effect by 20
3Change x by 15
4Wait 0.3 seconds
5Repeat steps 2 to 4 five more times
2
How many times does the sprite say “Hi!”, and how long does the program take?
whenclickedsayHi!for1secondswait1secondsrepeat4
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.
whenclickedset size to50%change size by10wait0.5secondsrepeat2
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.