GoalExplain what iteration is, why a loop needs a way to stop, and tell count-controlled loops from condition-controlled loops.
New words
iteration · repeating steps; another word for a looploop · a part of an algorithm that is run again and againinfinite loop · a loop that has no way to stop, so it goes on for evercount-controlled loop · a loop that runs a set number of times
Explanation
Iteration means repeating. A loop is the part of an algorithm that is run again and again. Without a loop you would have to write the same line many times, and changing the number of repeats would mean writing or deleting lines. Every loop needs a way to stop. A loop with no way to stop is an infinite loop: it keeps going until someone stops the program or it breaks down. A loop is stopped by a condition, which is a question the computer answers with true or false. A count-controlled loop repeats a number of times that is fixed in advance, for example ‘stir 12 times’. A condition-controlled loop keeps going while, or until, a condition is true, for example ‘stir until the sugar has gone’. A count-controlled loop uses a variable called a counter, which goes up by 1 each time round.
Without a loop
With a loop
OUTPUT "Splash"
FOR count ← 1 TO 4
OUTPUT "Splash"
OUTPUT "Splash"
OUTPUT "Splash"
NEXT count
OUTPUT "Splash"
(the same four lines of output)
Worked examples
Instruction
What stops it
Kind of loop
Tap the table 10 times
the count reaches 10
count-controlled
Keep tapping until the bell rings
the bell
condition-controlled
Water each of the 6 pots
all 6 are done
count-controlled
Keep walking until you reach the wall
the wall
condition-controlled
Everyday instructions can be sorted by how they stop. If you know the number of repeats before you start, the loop is count-controlled. If it depends on something that happens, it is condition-controlled.
Instruction
Has a way to stop?
Keep clapping
no: infinite loop
Clap 8 times
yes: count
Clap until the teacher raises a hand
yes: condition
The instruction ‘Keep clapping’ has no way to stop, so it is an infinite loop. Adding a stopping rule, either ‘clap 8 times’ or ‘clap until the teacher raises a hand’, fixes it. A real program with an infinite loop has to be stopped by someone from outside.
After jump number
jumps holds
start
0
1
1
2
2
3
3
The counter. A jump-rope game uses a variable called jumps that starts at 0. After each jump the program adds 1, which is called incrementing. After 3 jumps the counter holds 3.
Class activity
Tap, snap, stamp (no computer). Write three actions on paper, such as tap the table, snap your fingers and stamp a foot. Your partner holds up fingers as a counter: first a number card such as 5. Repeat the three actions that many times, with your partner raising one more finger after each round. Stop when the counter reaches the number card. Then change the rule: do the actions until your partner claps. Which version is count-controlled and which is condition-controlled? What would happen if your partner never clapped?
Practice
1
Sort each instruction as count-controlled or condition-controlled.
Instruction
Kind of loop
(a) Do 20 star jumps
?
(b) Keep pouring until the tank is full
?
(c) Run 7 laps
?
(d) Dance until the music stops
?
(a) count-controlled (20 is known in advance). (b) condition-controlled (stops when the tank is full). (c) count-controlled (7 laps). (d) condition-controlled (stops when the music stops).
2
A learner writes: ‘Keep stacking cups.’ What is wrong with this instruction? Give two different ways to correct it.
It has no way to stop, so it is an infinite loop. Correct it with a count (‘Stack 9 cups’, count-controlled) or with a condition (‘Keep stacking cups until the tower is as tall as your shoulder’, condition-controlled).
3
A counter called steps starts at 0 and goes up by 1 each time round a loop. What does steps hold after 4 times round? After 9 times round?
Times round the loop
steps holds
0 (start)
0
4
?
9
?
After 4 times round it holds 4, and after 9 times round it holds 9. The counter holds the number of times round so far.
4
Why is a loop better than writing OUTPUT "Ready" fifty times in a row? Give two reasons.
The loop needs only a few lines, so it is quicker to write and read. To change the number of repeats you change one number, not fifty lines, and there are fewer chances to make a mistake.