GoalRead and write a FOR loop in pseudocode and say its counter, start value, end value and how many times it runs.
New words
counter · the variable that counts the times round a FOR loopstart value · the number the counter has the first time roundend value · the last number the counter takes: the loop runs for it, and then stopsinclusive · including the end numbers, as in ‘3 to 6 inclusive’, which is 3, 4, 5 and 6
Explanation
A FOR loop is the most common count-controlled loop. Its first line has the shape FOR counter ← start TO end. The lines below it, down to NEXT counter, are the body, and they are indented. The counter has the start value first time round and goes up by 1 each time. The loop runs for every value from the start to the end, inclusive, so the end value is not the number of times round. FOR lap ← 3 TO 6 runs 4 times, for 3, 4, 5 and 6, which is end − start + 1. After the counter passes the end value the loop stops and the program carries on with the line after NEXT. That line runs only once. Some people write END FOR instead of NEXT lap, or = instead of ←. This week we use NEXT.
Pseudocode
1OUTPUT "Start"2FOR lap ← 1 TO 33 OUTPUT "Lap ", lap
4NEXT lap
5OUTPUT "Finish"
Output
Start
Lap 1
Lap 2
Lap 3
Finish
Worked examples
Part
In the algorithm
counter
lap
start value
1
end value
3
body (runs each time)
OUTPUT "Lap ", lap
end of loop
NEXT lap
runs once
OUTPUT "Start" and OUTPUT "Finish"
The parts of the loop in the picture above. The counter is lap. It starts at 1 and ends at 3, so the body runs 3 times. OUTPUT "Start" and OUTPUT "Finish" are not in the loop, so each runs once.
The start value need not be 1. Here the counter seat starts at 5, so the first output is Seat 5. It runs for 5, 6, 7 and 8, which is 8 − 5 + 1 = 4 times.
The counter can be used in a calculation. A pupil saves 5 coins more each week than the week before: the amount shown is week * 5. The output is Week 1: 5, Week 2: 10, Week 3: 15 and Week 4: 20.
Class activity
Be the FOR loop (no computer). Write the numbers 1 to 8 on separate pieces of paper and put them in a line on the floor. Write FOR box ← 3 TO 6 on a card. One person is the counter and stands on the start number. The other person is the body: each time the counter is on a number, they say ‘Box’ and that number. Then the counter steps up by 1. Stop as soon as the counter steps past the end value. Count how many times the body ran. Try FOR box ← 2 TO 2 and FOR box ← 1 TO 8 as well.
Practice
1
Predict the output of this algorithm.
Pseudocode
1FOR box ← 2 TO 52 OUTPUT box * 33NEXT box
FOR box ← 2 TO 5 runs for 2, 3, 4 and 5. The output is 6, 9, 12 and 15, each on its own line.
2
Write a FOR loop for each. (a) Output the numbers 7 to 12. (b) Output the word Hello nine times.
(a) FOR n ← 7 TO 12 / OUTPUT n / NEXT n. (b) FOR count ← 1 TO 9 / OUTPUT "Hello" / NEXT count. For (b) any start and end that are 9 values apart in the count are right, for example 0 TO 8.
3
How many times does each loop run? Write the number.
Loop header
Runs
(a) FOR i ← 1 TO 8
?
(b) FOR i ← 0 TO 8
?
(c) FOR i ← 3 TO 7
?
(d) FOR i ← 10 TO 10
?
(a) 8 times (1 to 8). (b) 9 times (0 to 8 includes 0). (c) 5 times (3, 4, 5, 6, 7). (d) 1 time (10 to 10 is one value).
4
A learner says: ‘FOR c ← 0 TO 4 runs 4 times, because the end value is 4.’ Explain what is wrong.
The loop runs for every value from 0 up to and including 4: that is 0, 1, 2, 3 and 4, which is 5 times. The end value is the last value of the counter, not the number of times round.