in BASIC ROM:
|List of all BASIC keywords|
Typ: Command General Programming-Syntax: NEXT [<Variable>][,<Variable>]…
The BASIC command NEXT is used with the BASIC-Command FOR. NEXT is the end of a FOR…TO…STEP…NEXT-loop. FOR- NEXT loops may be nested. The inclusion of the loop-variable after NEXT isn't necessary if referring to the inner-most loop. Every FOR statement must be paired with a NEXT statement.
Using NEXT when there isn't an active FOR running produces the BASIC-error ?NEXT WITHOUT FOR ERROR IN line. The maximum of open FOR-Loops are 9, Opening the 10th FOR-loop results in the BASIC-error ?OUT OF MEMORY ERROR IN line.
When the BASIC-Command NEXT is executed, the loop-counter of the FOR-Loop is incremented by the value of STEP (1 if there is no STEP included and decremented if STEP is negative). A FOR-Loop will be finished, when the loop-counter goes past the TO value.
Printed the numbers 1 until 15
10 FOR X=1 TO 15: PRINT X: NEXT
The same example, but NEXT without the variable
10 FOR X=1 TO 10 20 FOR Y=1 TO 10: PRINT X*Y;: NEXT: PRINT: NEXT
NEXT without a variable - example of the small multiplication tables
10 REM Colors 20 FOR X=0 TO 255 30 FOR Y=0 TO 255 40 POKE 53280,X: POKE 53281,Y: 50 FOR A=0 TO 150: REM Delay-loop 60 NEXT A,Y,X
Example with 3 Loops
10 FOR X=0 TO 1E17 20 PRINT X;".Loop - a number, please";:INPUT A$ 30 X=INT(VAL(A$)) 40 IF X=64 THEN X=1E18 50 NEXT
A endless loop, with a trick in line 40 that exits the loop when X=64.
Early exits from FOR-NEXT loops
When a program encounters a FOR statement, BASIC pushes certain information about the loop onto the C64 return stack which is used by the NEXT statement. This information includes the loop-variable, step size and where in the program the FOR was encountered. When FOR-NEXT finishes normally, this information is removed from the stack.
However, if the loop was exited early then the information remains on the return stack. Too many unfinished FOR-NEXT loops will then result in an ?OUT OF MEMORY ERROR. Because of the way that C64 BASIC handles FOR-NEXT information on the return stack, puzzing bugs can be the result.
C64 BASIC deals with information about FOR-NEXT loops on the return stack in one of 4 ways:
- When the FOR-NEXT loop terminates normally, the information is removed from the return stack.
- When a program encounters a NEXT statement, all inner-loops are terminated and their information removed from the return stack.
- All FOR-NEXT loops commenced within a subroutine are terminated and their information removed from the return stack when the program encounters a RETURN statement.
- If a FOR statement is encountered, any existing loop using the same variable name along with any subsequent unfinished FOR-NEXT loops are terminated and their information removed from the return stack.
The fourth rule usually prevents the ?OUT OF MEMORY ERROR since loop-variables tend to be re-used in programs. However, sometimes an inner loop gets terminated when the programmer didn't want it to be resulting in the ?NEXT WITHOUT FOR ERROR. Because of rule 2, this will also happen if the NEXT statements are accidentally transposed in nested loops.
A statement like
100 FOR I=1 TO 1:NEXT I
can be used to force the termination of a loop that was exited early.