A statement initialising a FORNEXT loop. The loop is executed at least once.

FOR temperature%=0 TO 9
FOR A(2,3,1)=9 TO 1 STEP -0.3

The FORNEXT loop is a way of repeating a section of program a set number of times. For example, the two programs below perform identically, but the second is easier to understand.

10 start=4: end=20: step=2
20 counter=start
30 PRINT counter," ",counter^2
40 counter=counter+step
50 IF counter<=end THEN 30
60 ...
10 start=4: end=20: step=2
20 FOR counter=start TO end STEP step
30   PRINT counter," ",counter^2
50 ...

You can GOTO anywhere within one FORNEXT loop, but not outside it. This means you can't exit the loop with a GOTO. You can force a premature end to the loop with EXIT FOR or by setting the control variable to a value equal to or greater than the end value (assuming a positive STEP).

110 FOR I=1 TO 20
120   X=A^I
130   IF X>1000 THEN I=20: GOTO 150
140   PRINT I,X
150 NEXT

It is not necessary to declare the loop variable as an integer type in order to take advantage of fast integer arithmetic. If it is an integer, then fast integer arithmetic is used automatically.

Because a single stack is used, you cannot use a FORNEXT loop to set array elements to LOCAL in a procedure or function.


FOR <n-var>=<numeric> TO <numeric> [STEP <numeric>]

Associated Keywords