Skip to content

Doing It Again

A loop repeats work without writing it out over and over, a set number of times, with a counter you can use as it goes.

  • Foundations

To show the numbers 1 to 10 with what you know so far, you would write ten SHOW lines. For 1 to 100, a hundred of them. Computers exist so that you never have to.

A loop does a piece of work over and over, as many times as you say, and you write that work once.

We use teaching notation to trace on paper, not commands to paste into a machine. A variable is a named place holding a value; SHOW displays it. The loop will give a variable a series of values without us assigning each one.

Count, without writing it out

Code
FOR i = 1 TO 10
  SHOW i
END
Output
1
2
3
4
5
6
7
8
9
10

FOR i = 1 TO 10 starts a loop with a counter called i. END marks where the repeated work stops. The computer runs everything between them with i set to 1, then runs it again with i set to 2, and keeps going up to 10.

Here the endpoints are whole numbers, and the counter increases by one after each completed visit. Both endpoints are included when the start is not past the end. If the start is already greater than the end, the body runs no times. Keep the endpoints and counter unchanged inside these introductory loops. A real language explains its own loop rules and spelling.

Which counter values are visited by 1 TO 0, 1 TO 1 and 0 TO 3? How many visits does each range give?

Show the explanation

1 TO 0 visits none: the start is already past the end. 1 TO 1 visits 1 once. 0 TO 3 visits 0, 1, 2, 3: four visits, including zero. For an ascending range like this, count both endpoints; subtracting 0 from 3 counts three gaps between values, rather than four values.

Three lines did the work of ten. Changing that 10 to 100 would do a hundred without another keystroke. That is the point of a loop: how much repetition you get is a number you set, not lines you type.

The counter is a value you can use

The counter is not only for keeping count. It is a variable, holding a different value each time round, and you can use it in the work:

Code
FOR i = 1 TO 10
  SHOW i, " times 7 is ", i * 7
END
Passii * 7Shown
1171 times 7 is 7
22142 times 7 is 14
33213 times 7 is 21
10107010 times 7 is 70

Ten visits to the same SHOW line. Only i changes, and the computer works i * 7 out afresh from it each time round.

A loop that counts and hands you the count is how you fill a row, draw a grid or lay out a level: anywhere the work is the same thing, once per number.

Which lines repeat?

The body is the work between FOR and its matching END. Indentation helps us see that boundary:

Code
SHOW "Starting"
FOR i = 1 TO 3
  SHOW i
END
SHOW "Done"

The output is Starting, then 1, 2, 3, then Done, each on its own line. In this straight-line example, the two messages outside the loop each appear once. Even if the range is empty, we still reach the message after END.

Move SHOW Done just above END, keeping its quotation marks. How many Done messages appear for 1 TO 3? What about 1 TO 0?

Show the explanation

Inside the body, SHOW "Done" runs three times for 1 TO 3, after each counter value. For 1 TO 0, it runs zero times, because the whole body is skipped. Outside, after END, it runs once in either example. Moving a line across the loop boundary changes the repetition; changing indentation alone does not move the END marker in our notation.

For a machine example, Give the answer a rhythm explains a three-dot loop in Sinclair BASIC. Its “Describe the repetition once” section recalls the counter; BASIC uses NEXT i where we use END. The later sound is after the loop, so it happens once. Read those explanations without playing the preceding game; running the complete program is optional.

When it’s wrong, see why

  • It runs once, or not at all. Check the range. FOR i = 1 TO 1 runs once. FOR i = 1 TO 0 runs no times, because the start is already past the end.
  • The numbers are out by one. 1 TO 10 gives ten passes. 0 TO 10 gives eleven. Decide whether you are counting things or counting from zero, then set the range to match.
  • Only part of the work repeats. The repeated work is everything between FOR and END. In the straight-line example above, a line after END runs once, even when the loop body runs no times.

What you’ve learnt

  • A loop repeats work without you writing it out. How many times is a value you set.
  • A counted loop runs a fixed number of times and keeps a counter that climbs as it goes.
  • The counter is a variable you can use in the repeated work. That is what makes a loop build things rather than only count.

What’s next

A counted loop is for when you know how many times. Often you do not: you repeat until something happens. In Unit 2 we meet that loop, and use it to turn the guessing game into something you can play.