NEXT

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: NEXT
Abbreviation: N Shift+E
Type: Command
Token code: 130/$82
Handling routine
in BASIC ROM:
44318
$AD1E
List of all BASIC keywords


Remark: This article describes the BASIC command NEXT in BASIC V2 at the Commodore 64.

Typ: Command
General Programming-Syntax: NEXT [[<Variable>[,<Variable>…]]

The BASIC command NEXT is used with the BASIC command FOR. NEXT denotes the "end" of a FOR…TO…STEP…NEXT loop construction and is usually paired with a corresponding FOR statement. It is not necessarily the end of the loop block, furthermore the statement could appear multiple times at any place. The inclusion of the loop variable after NEXT isn't necessary if referring to the inner-most loop. FOR-NEXT loops could be nested and with a single NEXT multiple loops could be closed by stating a comma-separated list of loop variables as NEXT parameters. An explicitly given variable name would force to abandon all nested and still open FOR loops, which might help in difficult structured program flows to ensure a safe program execution.

If a loop is left by GOTO the loop's administrative stack-frame remains on the stack, but will be reused if the a FOR with the same loop variable is entered (which also would remove all open nested FOR loops).
In addition, a RETURN from subroutine silently "closes" all opened FOR loops since the last GOSUB. See further details.

Using NEXT when there isn't an active FOR running produces the BASIC error ?NEXT WITHOUT FOR ERROR. The maximum of open FOR-Loops are 9 at best (depending on the current stack usage). Opening too many nested FOR loop results in the BASIC error ?OUT OF MEMORY ERROR.

The BASIC command NEXT adds the STEP value to the loop counter, which is 1 if there is no STEP given. A FOR-Loop will be finished, as soon the loop counter goes past the TO value or reaches the value in case the STEP value is 0.

Early exits from FOR-NEXT loops[edit | edit source]

C64 BASIC permits both changing the loop-variable and using GOTO to jump out of a FOR-NEXT loop early. However, doing the latter creates a potential problem.

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 (the FOR-NEXT stack frame). 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. A depth of 10 nested loops can be achieved at best (fit on the return stack), but depending on the stack usage before and after the practical limit is 9 or even less if some arithmetic expression evaluation are expected to be still possible. Because of the way that C64 BASIC handles FOR-NEXT information on the return stack, puzzling behavior may arise.

C64 BASIC deals with information about FOR-NEXT loops on the return stack in one of four ways:

  1. When the FOR-NEXT loop terminates normally, the information is removed from the return stack.
  2. When a program encounters a NEXT statement with an explicit loop variable, all inner loops are cancelled and their information removed from the return stack.
  3. 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.
  4. 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 reused 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 with loop variable I (inclusive all nested loops) that was exited early.[1]

Examples[edit | edit source]

10 FOR X=1 TO 15: PRINT X: NEXT X

Printed the numbers 1 until 15

10 FOR X=1 TO 15: PRINT X: NEXT

The same example, but NEXT without the loop variable

10 FOR X=1 TO 10
20 FOR Y=1 TO 10: PRINT X*Y;: NEXT: PRINT: NEXT

NEXT without a variable - generates a small multiplication table

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 three nested loops.
Implementation side-note: NEXT:NEXT:NEXT is executed faster than NEXT A,Y,X!

10 FOR X=0 TO 1 STEP 0: REM will never terminate
20 PRINT X;".Loop - a number, please";:INPUT A$
30 X=INT(VAL(A$))
40 IF X=64 THEN X=1 : REM exit
50 NEXT

An endless loop with a trick in line 40 to force the loop to exit if X=64.

1000 FOR W=-1 TO 0: GET A$: W=A$="": NEXT

Wait for a key-press without using GOTO.
This is an example for a "while" loop: The loop terminates as soon variable W get a value indicating a "false", set by the expression (A$=""). While this expression evaluates to true (-1), as long no key is pressed, it's keep running and waiting.

References[edit | edit source]


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT