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.
Format
The loop
Counter takes
TO and NEXT
FOR row ← 1 TO 4 / OUTPUT "Row ", row / NEXT row
1, 2, 3, 4
TO and END FOR
FOR row = 1 TO 4 / OUTPUT "Row ", row / END FOR
1, 2, 3, 4
indent only
FOR row ← 1 TO 4 / (indented) OUTPUT "Row ", row
1, 2, 3, 4
IN RANGE
FOR row IN RANGE(1, 5) / OUTPUT "Row ", row
1, 2, 3, 4
Worked examples
Python
1for row inrange(1, 5):
2print("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.
Loop
Counter takes
Runs
IN RANGE(1, 5)
1, 2, 3, 4
4
IN RANGE(0, 3)
0, 1, 2
3
IN RANGE(5, 8)
5, 6, 7
3
IN RANGE(2, 10)
2, 3, 4, 5, 6, 7, 8, 9
8
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 format
RANGE format
FOR n ← 3 TO 7
FOR n IN RANGE(3, 8)
FOR n ← 0 TO 4
FOR n IN RANGE(0, 5)
FOR n ← 6 TO 6
FOR n IN RANGE(6, 7)
FOR n ← 2 TO 9
FOR 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
The counter takes 2, 3, 4 and 5 (the 6 is not used), so the output is 12, 13, 14 and 15, each on its own line.
2
Write this loop in IN RANGE format: FOR k ← 5 TO 9 / OUTPUT k / NEXT k.
FOR k IN RANGE(5, 10) / OUTPUT k. The counter takes 5, 6, 7, 8 and 9, the same as the original, so 10 is the stop value.
3
Write this loop in TO and NEXT format: FOR n IN RANGE(0, 4) / OUTPUT n.
FOR n ← 0 TO 3 / OUTPUT n / NEXT n. Both give 0, 1, 2 and 3.
4
Three of these loops are meant to do the same job. Which loop is different, and how many times does each loop run?
Loop
Written
A
FOR c ← 1 TO 6
B
FOR c IN RANGE(1, 7)
C
FOR c IN RANGE(1, 6)
A runs 6 times (1 to 6) and B runs 6 times (1 to 6). C runs only 5 times (1 to 5), because the stop value 6 is not used. C is the odd one out.