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

FOR

Keyword Abbreviation Token (hex) Version(s) Classification
FOR F{Shift+O} 81 1.0 to 7.0 Command and Statement

 
Syntax  
FOR variable = initial TO final STEP delta ] [ : statement ] ...
[ statement : ] ... NEXT [ variable [ , variable ] ... ]
 
Parameters Type Legal Value(s) Default Value Note(s)
variable Float    all   Must be the name of a variable (not a literal number)
initial Float all
final Float all Should be different than initial
delta Float all 1.0 Should 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
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
Comments