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

Loops in new clothes

Week 3 · 1.2 Iteration · 3 lessons
1

Many ways to write FOR

Computing 9: Topic 1.2, pp. 25–45
GoalRead FOR loops written in different pseudocode formats and say which numbers the counter takes in each.
New words
format · the way an algorithm is laid out and which command words are usedrange · a way of writing a loop’s numbers as a start value and a stop value, as in RANGE(1, 5)exclusive · not including: in RANGE(1, 5) the stop value 5 is not usedinclusive · including: in FOR row ← 1 TO 4 the end value 4 is used
Explanation

Pseudocode has no set format, so the same loop can be written in several ways. Some writers end the loop with END FOR, some with NEXT and the name of the counter, and some write no end line and use only the change of indent to show where the loop stops. Assignment can be an arrow or an equals sign. A fourth format is FOR counter IN RANGE(1, 5), which looks like Python. In RANGE the start value is used but the end value is exclusive: the counter never takes the value 5, so the last number used is 4. To see the last number, subtract 1 from the number in RANGE. In the TO formats the end value is inclusive. All the loops in the picture give the same output. Choose one format and keep to it, but be ready to read the others.

FormatThe loopCounter takes
TO and NEXTFOR row ← 1 TO 4 / OUTPUT "Row ", row / NEXT row1, 2, 3, 4
TO and END FORFOR row = 1 TO 4 / OUTPUT "Row ", row / END FOR1, 2, 3, 4
indent onlyFOR row ← 1 TO 4 / (indented) OUTPUT "Row ", row1, 2, 3, 4
IN RANGEFOR row IN RANGE(1, 5) / OUTPUT "Row ", row1, 2, 3, 4
Worked examples
Python
1for row in range(1, 5):
2    print("Row", row)
Output
Row 1
Row 2
Row 3
Row 4
The RANGE format is close to a real Python loop. Here is the Python version, which was run to get the output. Range stops before 5, so the last row is 4.
LoopCounter takesRuns
IN RANGE(1, 5)1, 2, 3, 44
IN RANGE(0, 3)0, 1, 23
IN RANGE(5, 8)5, 6, 73
IN RANGE(2, 10)2, 3, 4, 5, 6, 7, 8, 98
Reading a RANGE: the counter takes every whole number from the first number up to, but not including, the second. The number of times round is the second number minus the first.
TO formatRANGE format
FOR n ← 3 TO 7FOR n IN RANGE(3, 8)
FOR n ← 0 TO 4FOR n IN RANGE(0, 5)
FOR n ← 6 TO 6FOR n IN RANGE(6, 7)
FOR n ← 2 TO 9FOR n IN RANGE(2, 10)
Changing between the two formats. To write a TO loop as a RANGE, add 1 to the end value. To write a RANGE as a TO loop, subtract 1 from the stop value.
Class activity

Format match (no computer). In a group of three, one person writes a loop in TO and NEXT format on a card, for example FOR gate ← 2 TO 5. The second writes it in IN RANGE format and the third in END FOR format, each on a different card. Mix all the cards from several groups. Sort them into piles that give the same numbers. Then each group writes a trap card, for example IN RANGE(2, 5) for the loop above, and the others must find which card is the odd one out. If you have a computer or tablet, an adult can help you check one RANGE loop in Python.

Practice
1
Follow the loop. What is output?
Pseudocode
1FOR counter IN RANGE(2, 6)
2    OUTPUT counter + 10
2
Write this loop in IN RANGE format: FOR k ← 5 TO 9 / OUTPUT k / NEXT k.
3
Write this loop in TO and NEXT format: FOR n IN RANGE(0, 4) / OUTPUT n.
4
Three of these loops are meant to do the same job. Which loop is different, and how many times does each loop run?
LoopWritten
AFOR c ← 1 TO 6
BFOR c IN RANGE(1, 7)
CFOR c IN RANGE(1, 6)