FOR
BASIC keyword | |
Keyword: | FOR |
Abbreviation: | F Shift+O |
Type: | Command |
Token code: | -/$- |
Handling routine in BASIC ROM: |
-–- $-–- |
List of all BASIC keywords |
Remark: This article describes the BASIC-Command FOR in BASIC V2 at the Commodore 64.
Type: Command General Programming-Syntax: FOR <Counter-Variable>=<Startnumber> TO <Endnumber> [ STEP <step-size-number>] NEXT
The BASIC command FOR is the start command of a FOR…TO…STEP…NEXT loop. This FOR...NEXT loop is executed until counter variable equals the value in the TO clause. With the step-size-number, the counter variable value is either increased (positive) or decreased (negative). When the STEP command isn't used then the step-size-number defaults to 1. Note that the counter variable is set <Endnumber> + <step-size-number> after the last iteration. A FOR...NEXT loop is used to execute the same BASIC commands one or more times. One advantage is that with lesser BASIC-code the same result is arrived:
10 REM Instead of 20 PRINT "1. OUTPUT OF THIS LINE" 30 PRINT "2. OUTPUT OF THIS LINE" 40 PRINT "3. OUTPUT OF THIS LINE" 50 PRINT "4. OUTPUT OF THIS LINE" 60 PRINT "5. OUTPUT OF THIS LINE"
10 REM The same using a FOR…NEXT loop 20 FOR X=1 TO 5 30 PRINT X". OUTPUT OF THIS LINE" 40 NEXT X
Note that this is not the only advantage. If you specify variables in place of starting and ending (TO) counter values then the number of iterations change dynamically according to variable values, calculated from user input etc. You could not do this dynamical behaviour just by repeated lines:
10 INPUT "HOW MANY DOTS DO YOU WANT";A 20 FOR I=1 TO A:PRINT".";:NEXT
The counter variable must be of a normal floating point or integer type, otherwise the BASIC error ?SYNTAX ERROR IN line (array variables) or ?TYPE MISMATCH ERROR IN line appears. Floating point variables can only store values from -1e+38 to 1e+38, otherwise the BASIC error ?OVERFLOW ERROR IN line is shown.
The closing command of a FOR...NEXT loop is the BASIC Command NEXT. The maximum of open FOR loops are 9, by opening the 10th FOR loop the BASIC error ?OUT OF MEMORY ERROR IN line occurs. FOR…NEXT-loops should not be interlaced (in the order FOR x...FOR y...NEXT x...NEXT y), because by too many opened FOR loops will raise a BASIC error to the programmer's surprise.
Examples[edit]
10 FOR X=1 TO 20: PRINT X: NEXT
The numbers 1 – 20 are printed.
10 FOR X=-10 TO 0 STEP 0.5: PRINT X: NEXT X
21 numbers will be printed from -10 to 0 with steps of 0.5.
10 FOR X=100 TO -100 STEP -25: PRINT X: NEXT X
9 numbers will be printed from 100 until -100 with steps of -25.
10 FOR X=10 TO 0 STEP -0.25: PRINT X: NEXT X
This loop will also decrease.
10 FOR X=1 TO 10: PRINT: FOR Y=1 TO 10: PRINT X*Y;:NEXT Y,X
Example of the small multiplication tables with 2 loops.
Early exits from FOR-NEXT loops[edit]
See the information about NEXT for a discussion about early exits from FOR-NEXT loops.
ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT