Contents / Structure within BASIC / Previous chapter / Next chapter / Index


15 FOR... NEXT

This structure makes the computer repeat a number of statements a fixed number of times. Try the following

10 FOR X=8 TO 20

20 PRINT X, X+X

30 NEXT X

>RUN

8 16

9 18

10 20

11 22

12 24

13 26

14 28

15 30

16 32

17 34

18 36

19 38

20 40

You can see that the computer looped through line 20 with X taking on the value 8, then 9, then 10 etc up to 20. Each time around, the loop X increased by 1. The "step size" can be changed easily.

10 FOR X= 8 TO 20 STEP 2.5

20 PRINT X, X+X

30 NEXT X

>RUN

8 16

10.5 21

13 26

15.5 31

18 36

In the two previous examples the value of X (which is called the "control variable") increased each time around the loop. The "control variable" can be made to decrease by using a negative step size.

10 FOR S= 100 TO 90 STEP -1

20 PRINT S,S/2, S/5

30 NEXT

>RUN

100 50 20

99 49.5 19.8

98 49 19.6

97 48.5 19.4

96 48 19.2

95 47.5 19

94 47 18.8

93 46.5 18.6

92 46 18.4

91 45.5 18.2

90 45 18

Here is a program which uses several FOR...NEXT loops. Some are 'nested' within each other in the way that one REPEAT...UNTIL loop was included within another.

10 FOR ROW = 1 TO 6

20 FOR STAR = 1 TO 10

30 PRINT"*";

40 NEXT STAR

50 FOR STRIPE = 1 TO 20

60 PRINT "=";

70 NEXT STRIPE

75 PRINT

80 NEXT ROW

100 FOR ROW = 1 TO 5

110 FOR STRIPE= 1 TO 30

120 PRINT"=";

130 NEXT STRIPE

140 PRINT

150 NEXT ROW

>RUN

**********====================

**********====================

**********====================

**********====================

**********====================

**********====================

==============================

==============================

==============================

==============================

==============================

The listing shown above is not very easy to follow - try typing

LISTO 7

and then re-listing the program.

>LISTO 7

>

>LIST

10 FOR ROW = 1 TO 6

20 FOR STAR = 1 TO 10

30 PRINT"*";

40 NEXT STAR

50 FOR STRIPE = 1 TO 20

60 PRINT "=";

70 NEXT STRIPE

80 PRINT

90 NEXT ROW

100 FOR ROW=1 TO 10

110 FOR STRIPE= 1 TO 30

120 PRINT"=";

130 NEXT STRIPE

140 PRINT

150 NEXT ROW

This causes each of the "nested" FOR...NEXT loops to be indented which can make it easier to follow.

Lines 20 to 40 print out 10 stars

Lines 50 to 70 print out 20 equal signs

and Lines 10 and 90 ensure that the above are repeated 6 times. Lines 100 to 150 print out 5 rows of 30 equal signs.

(With apologies to the USA for modifying their flag!)

A note on LISTO

LISTO stands for LIST Option and it is followed by a number in the range 0 to 7. Each number has a special effect and details are given on page 290. However, the two most useful values are 0 and 7.

LISTO 0 lists the program exactly as it is stored in memory

LISTO 1 lists the program with one space after each line number. Many programs in this book have been listed like this.

LISTO 7 lists the program with one space after the line number, and two extra spaces every time a FOR...NEXT loop or a REPEAT...UNTIL loop is detected.

If you are using the screen editor then make sure that you list the program with LISTO0 or else you will copy all those extra spaces into the line!

A few points to watch when using FOR...NEXT loops

  1. The loop always executes at least once

    10 FOR X= 20 TO 0

    20 PRINT X

    30 NEXT

    >RUN

    20

    The loop finishes with the "control variable" larger than the terminating value. In the next two examples the terminating value is 10.

    10 FOR Z= 0 TO 10 STEP 3

    20 PRINT Z

    30 NEXT

    40 PRINT "OUT OF LOOP"

    50 PRINT Z

    >

    >RUN

    0

    3

    6

    9

    OUT OF LOOP

    12

    10 FOR Z= 0 TO 10 STEP 5

    20 PRINT Z

    30 NEXT

    40 PRINT "OUT OF LOOP"

    50 PRINT Z

    >

    >RUN

    0

    5

    10

    OUT OF LOOP

    15

    Note that it is not necessary to say NEXT Z in line 30: it is optional, though it could be argued that it is clearer to put the Z in.

  2. You should NEVER jump out of a FOR...NEXT loop. It is well accepted that this is poor style. If you do this your programs will become extremely difficult to follow - there are always better alternatives usually involving the use of a procedure, or setting the control variable to a value greater than the terminating value for example.

    10 FOR X= 0 TO 1000

    15 PRINT

    20 PRINT "TYPE IN A SMALL NUMBER"

    30 PRINT "OR ENTER -1 TO STOP THE PROGRAM"

    40 INPUT J

    50 IF J=-1 THEN X= 2000

    60 PRINT "12 TIMES ";J;" IS "; 12*J

    70 NEXT X

    >

    >RUN

    TYPE IN A SMALL NUMBER

    OR ENTER -1 TO STOP THE PROGRAM

    ?32

    12 TIMES 32 IS 384

    TYPE IN A SMALL NUMBER

    OR ENTER -1 TO STOP THE PROGRAM

    ?456

    12 TIMES 456 IS 5472

    TYPE IN A SMALL NUMBER

    OR ENTER -1 TO STOP THE PROGRAM

    ?-1

    12 TIMES -1 IS -12

    The REPEAT...UNTIL loop provides a much better way of dealing with this sort of problem.

  3. If you omit the FOR statement an error will be generated. First a correct program:

    10 FOR X=1 TO 5

    20 PRINT "HELLO"

    30 NEXT

    >RUN

    HELLO

    HELLO

    HELLO

    HELLO

    HELLO

    and then the program with line 10 deleted

    20 PRINT "HELLO"

    30 NEXT

    >RUN

    HELLO

    No FOR at line 30

  4. Every FOR statement should have a matching NEXT statement. This can be easily checked by using LISTO 7 (list option 7). If the FOR...NEXT loops are correctly nested then the END in line 50 will line up with the FOR in line 5.

    5 FOR H= 1 TO 4

    10 FOR X=1 TO 2

    20 PRINT "HELLO" ,H,X

    30 NEXT X

    40 NEXT H

    50 END

    >LISTO 7

    >LIST

    5 FOR H= 1 TO 4

    10 FOR X=1 TO 2

    20 PRINT "HELLO", H,X

    30 NEXT X

    40 NEXT H

    50 END

    >RUN

    HELLO 1 1

    HELLO 1 2

    HELLO 2 1

    HELLO 2 2

    HELLO 3 1

    HELLO 3 2

    HELLO 4 1

    HELLO 4 2

    If the NEXT X in line 30 is deleted the computer does its best to make sense of the program.

    5 FOR H= 1 TO 4

    10 FOR X=1 TO 2

    20 PRINT "HELLO", H,X

    40 NEXT H

    50 END

    >RUN

    HELLO 1 1

    HELLO 2 1

    HELLO 3 1

    HELLO 4 1

    This is not the way to write programs! Mis-nested FOR...NEXT loops will cause problems.

  5. In summary FOR...NEXT loops should be used when you wish to go around a loop a fixed number of times.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001