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

Again and again

Week 2 · 1.2 Iteration · 3 lessons
1

Round and round

Computing 9: Topic 1.2, pp. 25–45
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 loopWith 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
InstructionWhat stops itKind of loop
Tap the table 10 timesthe count reaches 10count-controlled
Keep tapping until the bell ringsthe bellcondition-controlled
Water each of the 6 potsall 6 are donecount-controlled
Keep walking until you reach the wallthe wallcondition-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.
InstructionHas a way to stop?
Keep clappingno: infinite loop
Clap 8 timesyes: count
Clap until the teacher raises a handyes: 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 numberjumps holds
start0
11
22
33
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.
InstructionKind 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?
2
A learner writes: ‘Keep stacking cups.’ What is wrong with this instruction? Give two different ways to correct it.
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 loopsteps holds
0 (start)0
4?
9?
4
Why is a loop better than writing OUTPUT "Ready" fifty times in a row? Give two reasons.