Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

NEXT

Keyword Abbreviation Token (hex) Version(s) Classification
NEXT N{Shift+E} 82 1.0 to 7.0 Command, Preposition*, and Statement
*see RESUME for use as a preposition

 
 
Syntax  
FOR variable = initial TO final STEP delta ] [ : statement ] ...
[ statement : ] ... NEXT [ variable1 [ , variableN ] ... ]
 
Parameters Type Legal Value(s) Default Value Note(s)
variable1 Float    any except reserved variable most recent FOR variable Must be the name of a variable used by a preceding FOR
variableN
Float
any except reserved variable none Must be the name of a variable used by a preceding FOR

 
 
Purpose  
Program flow control.  Update and test variable(s) to decide if statement(s) will be executed again.
 
 
Remarks  
The named variable(s) following the NEXT keyword are optional.  If none are specified, the most recent variable specified by a preceding FOR is used.  When NEXT is reached, the value of variable (the default) or variable1 (user specified) is updated by adding delta to the variable(1).  Finally a test is made, based on delta and final:
  • If delta < 0 and variable >= final then the loop is repeated
  • If delta = 0 and variable <> final then the loop is repeated
  • If delta > 0 and variable <= final then the loop is repeated
  • Otherwise, continue with the statement after NEXT (if any)
If the loop does not continue (test fail), and another variable was specified ( , variableN ) then it will also be updated and tested like described above.  So for example NEXT X,Y is the same as NEXT X: NEXT Y.  In addition, if the variables X and Y were the most recent and next-to-most-recent variables used with FOR, then NEXT X,Y is also the same as NEXT : NEXT.  
 
If the specified variable1 (or variableN) is not the most-recent variable of a FOR statement, then the loop back to the most-recent FOR will be skipped, and execution will resume at the FOR statement with the specified variable name (assuming the test succeeded, otherwise the program continues at the statement following NEXT).
 
It is important to realise that besides skipping over a previous FOR, BASIC will also skip over any GOSUB entries on the stack.  Using the "wrong" variable name in a NEXT statment can thus royally foul up program flow control.  On the other hand, it sometimes used as a sneaky way to discard unwanted FOR or GOSUB entries residing on the BASIC stack.  This is mainly because only BASIC 4.7 contains the DISPOSE keyword which explicitly clears entries from the BASIC stack.
 
If the named variable1 (or variableN) does not correspond to any in use by a prior FOR statement (or there are no prior FOR statements), then a NEXT WITHOUT FOR error is generated.
 
Therefore, in typical cases, omitting the variable name(s) is recommended.
 
For ease in understanding (by humans) it is recommended that the variable names used in nested FOR loops be different.  However BASIC does not enforce this.  When consecutive (not nested) FOR loops are used, there is usually no harm in re-using the same variable name.
 
Example 1:
NEW
10 FOR X=1 TO 3
20 PRINT X
30 NEXT Y
RUN
 1

?NEXT WITHOUT FOR ERROR IN 30
READY.
30 NEXT
RUN
 1
 2
 3

READY.
 
Example 2:
NEW
10 FOR X=1 TO 3
20 :FOR Y=10 TO 20 STEP 10
30 :PRINT X,Y
40 :NEXT Y
50 NEXT X
RUN
 1       10
 1       20
 2       10
 2       20
 3       10
 3       20

READY.
The second example uses extra colons (:) to indicate the nesting of FOR statments.  This is rarely done in practice because it makes the program longer and slower.
 
 
Compare With  
 
See Also  

© H2Obsession, 2014
Comments