FOR
BASIC keyword | |
Keyword: | FOR |
Abbreviation: | F Shift+O |
Type: | Command |
Token code: | 129/$81 |
Handling routine in BASIC ROM: |
42818 $A742 |
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>=<startvalue> TO <endvalue> [ STEP <stepsize>]
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 <stepsize>, the counter variable value could be either increased if positive, greater than zero, decreased if negative or remain unchanged if 0. When the STEP command isn't used at all the step size defaults to 1. Note that the counter variable evaluates to <endvalue> + <stepsize> after the last iteration (except if the step value is 0 where the loop ends with the exact limit value <endvalue>).
The FOR loop is always entered, even the start value is beyond the end value, leading to a minimum of one iteration in every case. To overcome this limitation one has to prepend a conditional part to skip the loop if no iteration should take place.
A FOR...NEXT loop is used to execute the same BASIC commands one or more times. One advantage is to save program code size because the same result can be achieved with less BASIC program code:
Instead of
10 PRINT "1. OUTPUT OF THIS LINE" 20 PRINT "2. OUTPUT OF THIS LINE" 30 PRINT "3. OUTPUT OF THIS LINE" 40 PRINT "4. OUTPUT OF THIS LINE" 50 PRINT "5. OUTPUT OF THIS LINE"
the same, much shorter result is gained using a FOR…NEXT loop:
10 FOR X=1 TO 5 20 PRINT X". OUTPUT OF THIS LINE" 30 NEXT X
Note that this is not the only advantage. If you specify a variables or some calculating expression in place of constants for starting and ending (TO) values then the number of iterations might change dynamically according to the given values, derived from a user's input etc. You could not achieve such a dynamical behavior by repeating lines in code without changing the code accordingly:
10 INPUT "HOW MANY DOTS DO YOU WANT";A 20 FOR I=1 TO A:PRINT".";:NEXT
The counter has to be a simple floating point variable, no integer type variable is allowed, otherwise the BASIC error ?SYNTAX ERROR (for integer and array variables) or ?TYPE MISMATCH ERROR (for string variables) appears. Floating point variables can only store values in the range from approx. -1e+38 to 1e+38, otherwise the BASIC error ?OVERFLOW ERROR is shown.
The closing command of a FOR...NEXT loop is the BASIC Command NEXT. The maximum of open FOR-Loops are 9 at best (depending on the current stack usage). Opening too many nested FOR loop results in the BASIC error ?OUT OF MEMORY ERROR. FOR…NEXT loops should not be interlaced (in the order FOR x...FOR y...NEXT x...NEXT y), because the the NEXT x closes all inner loops (y) and the following NEXT y leads to a ?NEXT WITHOUT FOR ERROR.
Early exits from FOR-NEXT loops[edit | edit source]
C64 BASIC permits both changing the loop-variable and using GOTO to jump out of a FOR-NEXT loop early. However, doing the latter creates a potential problem.
When a program encounters a FOR statement, BASIC pushes certain information about the loop onto the C64 return stack which is used by the NEXT statement (the FOR-NEXT stack frame). This information includes the loop variable, step size and where in the program the FOR was encountered. When FOR-NEXT finishes normally, this information is removed from the stack.
However, if the loop was exited early then the information remains on the return stack. Too many unfinished FOR-NEXT loops will then result in an ?OUT OF MEMORY ERROR. A depth of 10 nested loops can be achieved at best (fit on the return stack), but depending on the stack usage before and after the practical limit is 9 or even less if some arithmetic expression evaluation are expected to be still possible. Because of the way that C64 BASIC handles FOR-NEXT information on the return stack, puzzling behavior may arise.
C64 BASIC deals with information about FOR-NEXT loops on the return stack in one of four ways:
- When the FOR-NEXT loop terminates normally, the information is removed from the return stack.
- When a program encounters a NEXT statement with an explicit loop variable, all inner loops are cancelled and their information removed from the return stack.
- All FOR-NEXT loops commenced within a subroutine are terminated and their information removed from the return stack when the program encounters a RETURN statement.
- If a FOR statement is encountered, any existing loop using the same variable name along with any subsequent unfinished FOR-NEXT loops are terminated and their information removed from the return stack.
The fourth rule usually prevents the ?OUT OF MEMORY ERROR since loop variables tend to be reused in programs. However, sometimes an inner loop gets terminated when the programmer didn't want it to be resulting in the ?NEXT WITHOUT FOR ERROR. Because of rule 2, this will also happen if the NEXT statements are accidentally transposed in nested loops.
A statement like
100 FOR I=1 TO 1:NEXT I
can be used to force the termination of a loop with loop variable I (inclusive all nested loops) that was exited early.[1]
Examples[edit | edit source]
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 in step of 0.5.
10 FOR X=100 TO -100 STEP -25: PRINT X: NEXT X
9 numbers will be printed from 100 until -100 in step of -25.
10 FOR X=10 TO 0 STEP -0.25: PRINT X: NEXT X
This loop count will also decrease.
10 FOR X=1 TO 10: PRINT: FOR Y=1 TO 10: PRINT X*Y;:NEXT Y,X
Example of generating the small multiplication table with two nested loops.
References[edit | edit source]
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