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.
Step
Instruction
1
Start when the green flag is clicked
2
Show the lamp ON for 1 second
3
Show the lamp OFF for 2 seconds
4
Repeat steps 2 and 3 forever
Worked examples
Step
Instruction
1
Start when the green flag is clicked
2
Say “Ready!” for 2 seconds
3
Turn the windmill sail 15 degrees right
4
Wait 0.1 seconds
5
Repeat 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.
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 Start
1 Start
2 Ring the bell once
2 Ring the bell once
3 Wait 5 seconds
3 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.
Step
Instruction
1
Start
2
Turn the light on for 1 second
3
Turn the light off for 1 second
4
Repeat steps __ and __ forever
Repeat steps 2 and 3 forever.
2
Which steps are done only once, and which steps repeat?
Step
Instruction
1
Start when the green flag is clicked
2
Put the fish in the middle
3
Turn the fish 10 degrees right
4
Wait 1 second
5
Repeat steps 3 and 4 forever
Steps 1 and 2 are done once. Steps 3 and 4 repeat. Step 5 is the instruction that makes them repeat.
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?
No, the places are random, so you cannot know. It has made 5 glides.
4
Does a program with a forever loop stop by itself? How can it be stopped?
No. A person stops it, for example by clicking the stop button.