FOR
From C64-Wiki
| 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.
Typ: Command General Programming-Syntax: FOR (<Counter-Variable>)=<Startnumber> TO <Endnumber> [STEP] <step-size-number>
The BASIC-Command FOR is the start-command for a FOR…TO…STEP…NEXT-loop. This FOR...NEXT...loop is executed until counter-variable in the term is true. With the step-size-number can be changed, if the loop is increased or decreased. When the STEP-command isn't used the step-size-number is +1. A FOR...NEXT-Loop is using for a number of the same BASIC-commands for calculation or screen- and printer-output. The advantage is, that with lesser BASIC-code the same 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 a FOR…NEXT-loop 20 FOR X=1 TO 5 30 PRINT X". OUTPUT OF THIS LINE" 40 NEXT X
The variable type must be a normal floating point or integre, contrariwise the BASIC-Error ?SYNTAX ERROR IN line (array variables) or ?TYPE MISMATCH ERROR IN line. In floating point variables can be only used number with values from -1e+38 until 1e+38, otherwise the BASIC-error ?OVERFLOW ERROR IN Zeilennummer is viewing.
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 din't interlaced, because by to many opened FOR-loops occured a BASIC-error or the programmer is amazed about something.
[edit] Examples
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
20 numbers will be printed from -10 until 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 decreased
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.
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
