English
Language : 

NSB8 Datasheet, PDF (103/158 Pages) List of Unclassifed Manufacturers – BASIC interpreter for Z80 family
ITALIAN TECHNOLOGY
grifo®
Example #2 will repeat {BODY} five times, with successive values of J being 1, 3, 5, 7, and 9. J is
increased by 2 after each iteration. In example #3, {BODY} is also repeated 5 times, but the value
of K(3) will decrease by 1 for each iteration.
If the STEP clause is not used in a FOR, then the step value is always assumed to be 1.
Note that, when the step value is positive, the initial value must be less than or equal to the limit
value. When the step value is negative, the initial value must be greater than or equal to the limit. If
these rules are not followed, {BODY} will never be executed, as in the next example:
Example #4
10 FOR Q=5 TO 1
20 PRINT “THIS LINE WILL NEVER BE EXECUTED”
99 NEXT
100 PRINT “PAST THE LOOP”
Running the above program yields only the message
PAST THE LOOP
on your terminal. In this case, line 20 is the body, but even before it can be executed, BASIC sees
that the value of Q is greater than 1, and that, with an implied step of 1, Q will never acquire the limit
value of 1, so it does not execute the body at all, and jumps down to line 100 to continue execution.
The initial, limit, and step value expressions in a FOR statement need not be integer in nature. Thus,
it is possible to have a loop such as
Example #5
10 FOR I=.1 TO 10.5 STEP .1
{BODY}
99 NEXT
100 REM Above loop will repeat 105 times.
Because I is a regular BASIC variable, its value may be compared with others or changed outright
during repetition, using the IF and LET statements, respectively. Changing the value of the control
variable, however, should be done with great care, and is an advanced technique not recommended
for the beginning programmer. It is not possible to change the initial, limit, or step values of the loop
during iteration. They are permanently set for the given loop when its FOR statement is first executed
(it is suggested that the control variable not be used in the limit or step expressions).
FOR LOOP NESTING
FOR loops may be executed while other FOR loops are already in progress. This is caled nesting of
FOR loops.
Example #6
10 FOR I=0 TO 9
20 FOR J=0 TO 9
30 PRINT I, J
40 NEXT
50 NEXT
NSB8
Rel. 5.10
Page 91