| 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 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 |
*see RESUME for use as a preposition
FOR variable = initial TO final [ STEP delta ] [ : statement ] ... [ statement : ] ... NEXT [ variable1 [ , variableN ] ... ]
Program flow control. Update and test variable(s) to decide if statement(s) will be executed again.
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 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:
Example 2:
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.
© H2Obsession, 2014 |