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
2

Count the laps

Computing 9: Topic 1.2, pp. 25–45
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 3
3    OUTPUT "Lap ", lap
4NEXT lap
5OUTPUT "Finish"
Output
Start
Lap 1
Lap 2
Lap 3
Finish
Worked examples
PartIn the algorithm
counterlap
start value1
end value3
body (runs each time)OUTPUT "Lap ", lap
end of loopNEXT lap
runs onceOUTPUT "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.
Pseudocode
1FOR seat ← 5 TO 8
2    OUTPUT "Seat ", seat
3NEXT seat
Output
Seat 5
Seat 6
Seat 7
Seat 8
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.
Pseudocode
1FOR week ← 1 TO 4
2    OUTPUT "Week ", week, ": ", week * 5
3NEXT week
Output
Week 1: 5
Week 2: 10
Week 3: 15
Week 4: 20
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 5
2    OUTPUT box * 3
3NEXT box
2
Write a FOR loop for each. (a) Output the numbers 7 to 12. (b) Output the word Hello nine times.
3
How many times does each loop run? Write the number.
Loop headerRuns
(a) FOR i ← 1 TO 8?
(b) FOR i ← 0 TO 8?
(c) FOR i ← 3 TO 7?
(d) FOR i ← 10 TO 10?
4
A learner says: ‘FOR c ← 0 TO 4 runs 4 times, because the end value is 4.’ Explain what is wrong.