NEXT

From C64-Wiki
Jump to: navigation, search
BASIC keyword
Keyword: NEXT
Abbreviation: N Shift+E
Type: Command
Token code: -/$-
Handling routine
in BASIC ROM:
-–-
$-–-
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 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.

Examples[edit]

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 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[edit]

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. 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:

  1. When the FOR-NEXT loop terminates normally, the information is removed from the return stack.
  2. When a program encounters a NEXT statement, all inner-loops are terminated 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 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.[1]


References[edit]

  1. Compute! Magazine November 1984
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