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.
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.
i
Does i still fit (i <= 2)?
Output
0
yes
Seed 1 planted
1
yes
Seed 2 planted
2
yes
Seed 3 planted
3
no
(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.
Loop
Body runs
Verdict
FOR c ← 1 TO 5
5
5 claps: correct
FOR c ← 1 TO 4
4
one too few
FOR c ← 0 TO 5
6
one too many
FOR c ← 0 TO 4
5
5 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?
Loop
Times the body runs
FOR a ← 4 TO 9
?
FOR b ← 0 TO 6
?
FOR c ← 8 TO 3
?
FOR d ← 5 TO 5
?
FOR a ← 4 TO 9: 6 times (4, 5, 6, 7, 8, 9). FOR b ← 0 TO 6: 7 times. FOR c ← 8 TO 3: 0 times, because the end is smaller than the start. FOR d ← 5 TO 5: once.
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 + 13NEXT lap
4. The counter runs 0, 1, 2, 3, 4, which is five times, and lap + 1 gives Lap 1 to Lap 5.
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
Four lines, because g takes 0, 1, 2 and 3. Fix 1: FOR g ← 0 TO guests - 1. Fix 2: FOR g ← 1 TO guests.
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?
INPUT stickers / FOR s ← 1 TO stickers / OUTPUT "Sticker ", s / NEXT s. If the user types 0, the loop is FOR s ← 1 TO 0, so the body never runs and nothing is output.