Chapter 12 - Looping

Suppose you want to input five numbers & add them together. One way (don't type this in unless you're feeling dutiful) is to write

10 LET TOTAL=0

20 INPUT A

30 LET TOTAL=TOTAL+A

40 INPUT A

50 LET TOTAL=TOTAL+A

60 INPUT A

70 LET TOTAL=TOTAL+A

80 INPUT A

90 LET TOTAL=TOTAL+A

100 INPUT A

110 LET TOTAL=TOTAL+A

120 PRINT TOTAL

This method is not good programming practice. It may be just about controllable for five numbers, but you can imagine how tedious a program like this to add ten numbers would be, & a hundred would be just impossible.

Much better is to set up a variable to count up to five & then stop the program, like this (which you should type in):

10 LET TOTAL=0

20 LET COUNT=1

30 INPUT A

40 REM COUNT = NUMBER OF TIMES THAT A HAS BEEN INPUT SO FAR

50 LET TOTAL=TOTAL+A

60 LET COUNT=COUNT+1

70 IF COUNT<=5 THEN GOTO 30

80 PRINT TOTAL

Notice how easy it would be to change line 70 so that this program adds ten numbers, or even a hundred.

This sort of counting is so useful that there are two special statements to make it easier: the FOR statement, & the NEXT statement. They are always used together. Using these, the program you have just typed in does exactly the same as

10 LET TOTAL=0

20 FOR C=1 TO 5

30 INPUT A

40 REM C = NUMBER OF TIMES THAT A HAS BEEN INPUT SO FAR

50 LET TOTAL=TOTAL+A

60 NEXT C

80 PRINT TOTAL

(To get this program from the previous one you just have to edit lines 20, 40, 60 & 70. TO is shifted 4.)

Note that we have changed COUNT to C. The counting variable - or control variable - of a FOR-NEXT loop must have a single letter for its name.

The effect of this program is that C runs through the values 1 (the initial value), 2, 3, 4 & 5 (the limit), & for each one, lines 30, 40 & 50 are executed. Then, when C has finished its five values, line 80 is executed.

An extra subtlety to this is that the control variable does not have to go up by 1 each time: you can change this 1 to anything else you like by using a STEP part in the FOR statement. The most general form for a FOR statement is

FOR control variable = initial value TO limit STEP step

where the control variable is a single letter, & the initial value, limit & step are all numeric expressions. So, if you replace line 20 in the program by

20 FOR C=1 TO 5 STEP 3/2

then C will run through the values 1, 2.5 & 4. Notice that you don't have to restrict yourself to whole numbers, & also that the control value does not have to hit the limit exactly - it carries on looping as long as it is less than or equal to the limit (but see exercise 4).

You must be careful if you are running two FOR-NEXT loops together, one inside the other. Try this program, which prints out a complete set of 6-spot dominoes.

 10 FOR M=0 TO 6         20 FOR N=0 TO M         30 PRINT M;":";N;" ";         40 NEXT N         50 PRINT         60 NEXT M N-loop     | N-loop M-loop     |     |     |     | M-loop

You can see that the N-loop is entirely inside the M-loop - they are properly nested. What must be avoided is two FOR-NEXT loops that overlap without either being entirely inside the other, like this:

 WRONG 10 FOR M=0 TO 6         20 FOR N=0 TO M         30 PRINT M;":";N;" ";         40 NEXT M         50 PRINT         60 NEXT N M-loop     |     | M-loop N-loop     |     |     | N-loop

The FOR-NEXT loops must either be one inside the other, or be completely separate.

Another thing to avoid is jumping into the middle of a FOR-NEXT loop from the outside. The control variable is only set up properly when its FOR statement is executed, & if you miss this out the NEXT statement will confuse the computer. You might get error report 1 or 2 (meaning that a NEXT statement does not contain a recognised control variable) if you're lucky.

Summary

Statements: FOR, NEXT, TO, STEP

Exercises

1. Rewrite the program in chapter 11 that prints out the character set, using a FOR-NEXT loop (Answer in chapter 13.)

2. A control variable has not just a name & a value, like an ordinary variable, but also a limit, a step, & a line number for looping back to (the line after the FOR statement where it was set up). Persuade yourself first, that when the FOR statement is executed all this information is available (using the initial value as the first value it takes), & second, that (using as an example our second & third programs), this information is enough to

convert the one line.

NEXT C

into the two lines

LET C=C+1

IF C<=5 THEN GOTO 30

(Actually we have cheated slightly here: it should really be GOTO 21 instead of GOTO 30. This will have the same effect in our program.)

3. Run the program, & then type

PRINT C

Why is the answer 6, & not 5?

[Answer: the NEXT statement in line 60 is executed 5 times, and each time 1 is added to C.] What happens if you put STEP 2 in line 20?

4. Change the third program so that instead of automatically adding five numbers, it asks you to input how many numbers you want adding. When you run this program, what happens if you input 0, meaning that you want no numbers adding? Why might you expect this to cause problems to the computer, even though it is clear what you mean? (The computer has to make a search for the statement NEXT C, which is not usually necessary.) In fact this has all been taken care of.

5. Try this program, to print out the numbers from 1 to 10 in reverse order.

10 FOR N=10 TO 1 STEP -1

20 PRINT N

30 NEXT N

Convert this into a program that does not use FOR-NEXT loops in the same way that you would convert program 3 into program 2 (see exercise 2). Why does the negative step make is slightly different?

Previous: Chapter 11    Next: Chapter 13