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

Repeat and reuse

Week 5 · 1.3 Creating algorithms · 3 lessons
1

How many times?

Computing 9: Topic 1.3, pp. 46–79
GoalWork out how many times a count-controlled loop runs, and choose start and end values that give the number of repeats you want.
New words
start value · the value the counter has the first time the loop body runsend value · the last counter value that still runs; the loop stops after itoff-by-one error · a mistake where a loop runs one time too many or one time too few because of a wrong start or end value
Explanation

A count-controlled loop repeats its body a fixed number of times. In FOR i ← a TO b the counter i begins at a. After each run it goes up by 1, and the loop ends once i has passed b. The end value b is the last one that still runs, so the body runs b − a + 1 times. If you start at 1, the end value is simply the number of repeats you want. If you start at 0, the end value must be one less than the number of repeats. A number typed by the user works in the same way, for example FOR i ← 1 TO n. When the end value is smaller than the start value, the body does not run at all. Choosing an end value that is one too big or one too small is called an off-by-one error.

StartEndCounter takes the valuesBody runs
151, 2, 3, 4, 55
040, 1, 2, 3, 45
373, 4, 5, 6, 75
5551
62(none)0
Worked examples
Pseudocode
1INPUT seeds
2FOR seed ← 1 TO seeds
3    OUTPUT "Seed ", seed, " planted"
4NEXT seed
Output
Seed 1 planted
Seed 2 planted
Seed 3 planted
A garden helper prints a label for every seed that is planted. The user types 3. The counter starts at 1, so the end value is the typed number, and the counter itself is printed. The body runs for 1, 2 and 3.
iDoes i still fit (i <= 2)?Output
0yesSeed 1 planted
1yesSeed 2 planted
2yesSeed 3 planted
3no(loop ends)
The same job with a counter that starts at 0. For 3 seeds the end value is seeds - 1, which is 2, and the label shows i + 1. The table follows the counter. After the value 2 has run, the counter becomes 3, the test 3 <= 2 fails, and the loop ends.
LoopBody runsVerdict
FOR c ← 1 TO 555 claps: correct
FOR c ← 1 TO 44one too few
FOR c ← 0 TO 56one too many
FOR c ← 0 TO 455 claps: correct
Off-by-one errors. The goal is exactly 5 claps. Rows 1 and 4 run the body exactly 5 times. Rows 2 and 3 are off by one, and counting the values of the counter shows it: 1 to 4 is only four values, and 0 to 5 is six.
Class activity

Bean cup (no computer). You need ten dried beans or buttons and a cup. Write a loop card such as FOR jar ← 2 TO 5. Your partner drops one bean in the cup for every run of the body, and says the counter value aloud each time. Count the beans and compare with end − start + 1. Try cards with a start of 0, a start of 1, and one where the end is smaller than the start (no beans!). Then swap roles and write a card that must drop exactly 6 beans, once starting at 0 and once starting at 1.

Practice
1
How many times does the body run in each loop?
LoopTimes the body runs
FOR a ← 4 TO 9?
FOR b ← 0 TO 6?
FOR c ← 8 TO 3?
FOR d ← 5 TO 5?
2
This loop should output Lap 1, Lap 2, Lap 3, Lap 4 and Lap 5. Which number must replace the question mark?
Pseudocode
1FOR lap ← 0 TO ?
2    OUTPUT "Lap ", lap + 1
3NEXT lap
3
The user types 3 guests and expects three Welcome lines. How many lines does this algorithm output? Correct it in two different ways.
Pseudocode
1INPUT guests
2FOR g ← 0 TO guests
3    OUTPUT "Welcome"
4NEXT g
4
Write pseudocode that asks for a number of stickers and outputs Sticker 1, Sticker 2 and so on up to that number. Start your counter at 1. What happens if the user types 0?