Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      Abs
      And
      Append
      Asc
      Atn
      Auto
      Backup
      Bank
      Begin
      Bend
      Bload
      Boot
      Box
      Bsave
      Bump
      Catalog
      Char
      Chr
      Circle
      Close
      Clr
      Cmd
      Collect
      Collision
      Color
      Concat
      Cont
      Copy
      Cos
      Data
      Dclear
      Dclose
      Dec
      Def
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      Instr
      Int
      Joy
      Key
      Left
      Len
      Let
      List
      Load
      Locate
      Log
      Loop
      Mid
      Monitor
      Movspr
      New
      Next
      Not
      Off
      On
      Open
      Or
      Paint
      Peek
      Pen
      Pi
      Play
      Pointer
      Poke
      Pos
      Pot
      Print
      Print num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      Rnd
      Rreg
      Rspcolor
      Rsppos
      Rsprite
      Run
      Rwindow
      Save
      Scale
      Scnclr
      Scratch
      Sgn
      Sin
      Sleep
      Slow
      Sound
      Spc
      Sprcolor
      Sprdef
      Sprite
      Sprsav
      Sqr
      Sshape
      St
      Stash
      Step
      Stop
      Str
      Swap
      Sys
      Tab
      Tan
      Tempo
      Then
      Ti
      Ti string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
NEXTN{Shift+E}821.0 to 7.0Command, Preposition*, and Statement
*see RESUME for use as a preposition

 
 
Syntax 
FOR variable = initial TO final STEP delta ] [ : statement ] ...
[ statement : ] ... NEXT [ variable1 [ , variableN ] ... ]
 
ParametersTypeLegal Value(s)Default ValueNote(s)
variable1Float   any except reserved variablemost recent FOR variableMust be the name of a variable used by a preceding FOR
variableN
Float
any except reserved variablenoneMust 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

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

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