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

Loops repeat for us

Week 1 · Unit 1: Be an animator · 3 lessons
2

Repeat steps forever

Computing 4: Unit 1, pp. 8–19
GoalChange an algorithm so that some of its steps repeat for ever.
New words
algorithm · a list of steps, in order, that does a jobforever loop · a loop that repeats the same steps over and over without stoppingrandom · chosen with no pattern, so you cannot guess it
Explanation

An algorithm is a list of numbered steps. Usually each step is done once and then the algorithm ends. To make some steps repeat, we add a step such as “Repeat steps 2 and 3 forever”. This makes a forever loop. The steps inside the loop run over and over, and the loop never ends by itself. Only a person can stop it, for example by clicking the stop button. Steps before the loop, such as setting the start place, are done once only. Some steps use random: the computer picks a place or a number that nobody can guess.

StepInstruction
1Start when the green flag is clicked
2Show the lamp ON for 1 second
3Show the lamp OFF for 2 seconds
4Repeat steps 2 and 3 forever
Worked examples
StepInstruction
1Start when the green flag is clicked
2Say “Ready!” for 2 seconds
3Turn the windmill sail 15 degrees right
4Wait 0.1 seconds
5Repeat steps 3 and 4 forever
Only the steps named in the repeat step go round and round. Here steps 1 and 2 are done once, so the windmill says “Ready!” only one time. Steps 3 and 4 repeat.
123?next?
A night-sky algorithm: 1 Start. 2 Show the star. 3 Jump to a random position. 4 Twinkle for half a second. 5 Repeat steps 3 and 4 forever. The star keeps popping up in new places, and nobody can guess where it will be next.
Before (runs once)After (runs forever)
1 Start1 Start
2 Ring the bell once2 Ring the bell once
3 Wait 5 seconds3 Wait 5 seconds
4 Repeat steps 2 and 3 forever
To change an algorithm that runs once into one that runs forever, add one new last step. The picture shows a school bell algorithm before and after.
Class activity

Wave the flag (with family or friends): follow this algorithm. 1 Stand with your arms down. 2 Raise both arms. 3 Count to 2. 4 Lower both arms. 5 Count to 2. 6 Repeat steps 2 to 5 forever. One person is the program and the others do the steps. The leader calls “Stop!” when they have had enough. Which step did you do only once? If you have a computer or tablet, find the forever block in the Control group of Scratch and put a “turn ↻ (15) degrees” block inside it.

Practice
1
Complete the algorithm for a flashing bike light: 4 Repeat steps __ and __ forever.
StepInstruction
1Start
2Turn the light on for 1 second
3Turn the light off for 1 second
4Repeat steps __ and __ forever
2
Which steps are done only once, and which steps repeat?
StepInstruction
1Start when the green flag is clicked
2Put the fish in the middle
3Turn the fish 10 degrees right
4Wait 1 second
5Repeat steps 3 and 4 forever
3
A bee glides 1 second to a random position, again and again forever. After 5 seconds, can you say exactly where the bee is? How many glides has it made?
x 240−240y 180−180start?
4
Does a program with a forever loop stop by itself? How can it be stopped?