Home > CBM > BASIC > Keywords > For
180 Subpages: (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, 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

KeywordAbbreviationToken (hex)Version(s)Classification
FORF{Shift+O}811.0 to 7.0Command and Statement

 
Syntax 
FOR variable = initial TO final STEP delta ] [ : statement ] ...
[ statement : ] ... NEXT [ variable [ , variable ] ... ]
 
ParametersTypeLegal Value(s)Default ValueNote(s)
variableFloat   all Must be the name of a variable (not a literal number)
initialFloatall
finalFloatallShould be different than initial
deltaFloatall1.0Should be negative if final < initial
statement
Command or
Statement
all  

 
 
Purpose 
Program flow control.  Execute statement(s) one or more times, and maintain a running counter.

 
 
Remarks 
The entire FOR...NEXT construct may be on a single line, or span multiple lines (not only 2 lines like Syntax shows).  The statement(s) will be executed at least once in Commodore BASIC.  (In some other versions, the statement(s) may be skipped entirely, depending on the parameters.)  FOR begins by setting the named variable to the initial value.  Then all the statements (if any) are executed.  When NEXT is reached, the value of variable is updated by adding delta to the variable.  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)
The initial, final, and delta values may be specified with variables of any numeric type.  If so, those variables will not be referenced again by FOR/NEXT.  This means during execution of the statement(s), any changes to those variables will have no effect on the loop.  However changing the variable will affect the loop.  Although BASIC will convert initial, final, and/or delta into floating-point format if they are integers, the variable must be floating-point type; SYNTAX ERROR will result otherwise.
 
BASIC assigns the initial value to variable before it processes the final and delta parameters.  This means final and delta should not reference the old value of variable.  For example, the following will not step from 1 to 8:
K = 8 : FOR K = 1 TO K : PRINT K : NEXT
 1

READY.
If the results puzzle you, know that BASIC is stepping from 1 to 1.  This is because the "TO K" refers to the new value of K set by "FOR K = 1".
 
Besides the main use of executing a set of statements multiple times, it is often used with no statements between FOR/NEXT to slow execution.  This use is not portable because the timing delay is machine-dependant.  However the delay is usually for the benefit of a human user and not for time-critical code, so porting such code will not cause the program to crash (althoug it may run too fast or slow for the comfort of the user).
 
FOR/NEXT is typically used when the number of loops is known in advance or can be easily calculated.  If this is not the case, other techniques are often used: typically IF/THEN/GOTO or DO/LOOP.
 
Example:
NEW

READY.
10 FOR X=1 TO 3
20 PRINT X
30 NEXT
40 FOR I=1 TO 1000: NEXT : REM Delay for about a second on most CBM machines
50 FOR X=3 TO 1 STEP -1
60 PRINT X
70 NEXT
RUN
 1 2 3(delay) 3 2 1

READY.


 
Compare With 
 
See Also 
NEXT, STEP, TI, TI$, TO 

© H2Obsession, 2014