FOR`NEXT loop is the most commonly used instruction in BASIC. The behavior of it is prescribed in the BASIC standards such as ANSI/ISO minimal BASIC, ANSI/ISO Full BASIC and ECMA BASIC. Knowing the precise behavior of FOR`NEXT loop, we can make concise and reliable programs.
A FOR`NEXT loop is written in the following form:
FOR control_var = initial_value TO limit STEP increment @@ ccc @NEXT control_var
limit and increment are evaluated before the repetition starts and stored in invisible variables.
When increment is a positive number, the repetition works as follows.
(1) Assigns initial_value to control_var.
(2) If control_var is greater then limit, the control goes to the next line of NEXT.
(3) When NEXT statement is executed, control_var is added increment and goes back ot (2).
When increment is a negative number, the magnitude relation in (2) is reversed.
When gSTEP incrementh is omitted, increment is regarded as 1.
When increment is a positive number, if initial_value is greater than limit, the repetition part is not executed and the FOR`NEXT loop finishes. control_var remains to be initial_value.
The condition of exitting the repetition is that the value of the control variable is greater than the limit. Thus a FOR`NEXT loop where the difference of the initial value and the limit is not a multiple of the increment can be allowed as fllows:
10 FOR i=0 TO 100 STEP 3 20 PRINT i 30 NEXT i
When the last time PRINT statement in line 20 is executed, the value of the varibale i is 99, and the value of i is 102 after exitting the loop.
When specifying decimals in increments, be careful of errors. For example, on the following,
10 FOR x=0 TO PI STEP PI/6 20 PRINT x 30 NEXT x
The last output is 2.6179938779915 but 3.141592c . This is because the value of this next x is 3.1415926535898, which is slightly larger than 3.14159265358979, so it goes through the repetition there.
One of the measures is to specify the limit a little higher. For example, on the following
10 FOR x=0 TO PI+1E-10 STEP PI/6
the last output is 3.1415926535898, which is almost as expected.
Another way is to use only integers for FOR sentences, do like following
10 FOR i=0 TO 6 20 LET x=PI*i/6 30 PRINT x 40 NEXT i
This method has an advantage in terms of accuracy as it does not accumulate errors.
In binary mode and complex mode, there is also an error in round decimals such as in 0.1. For example,
10 OPTION ARITHMETIC NATIVE 20 FOR x=1 TO 2 STEP 0.1 30 PRINT x 40 NEXT x 50 END
results 1.9 for the last.
Increases and limits are kept in variables that are invisible to the program. Therefore, in the following program the increment remains 0.1, despite rewriting the s value at line 40.
10 LET s=0.1 20 FOR i=1 TO 10 STEP s 30 PRINT i,s 40 IF i=1 THEN LET s=1 50 NEXT i 60 END
Also, the following execute the 30 line 10 times.
10 LET n=10 20 FOR i=1 TO n 30 PRINT i,n 40 IF i=4 THEN LET n=5 50 NEXT i 60 END
Rewriting the value of the control variable, as in the following, affects iterations.
10 FOR i=1 TO 100 20 PRINT i 30 IF i=20 THEN LET i=100 40 NEXT i
Mistakes using the same control variables as callers in internal sub-programs and internal function definitions are common failure.
Example
10 FOR i=1 TO 10 20 CALL ss 30 NEXT i 40 SUB ss 50 FOR i=1 TO 10 60 PRINT i 70 NEXT i 80 END SUB 90 END
Only 20 line will be executed once in this program. Because the value of variable i is 11 when I finish running the internal subprogram ss.
Means to avoid this kind of error are (1 ) using another variable name (2 ) using external procedures (3 ) Use the LOCAL sentence, which is not sepecified in Full BASIC standard. The most certain of these is the use of external procedures.
The EXIT FOR statement does not change the value of the control variable. Also, in BASIC, the control variable is a valid variable even if you exit the FOR`NEXT loop.
Therefore, by comparing the value of the control variable with the limit, you can determine whether you have run the EXIT FOR statement and have gone through iterations, or whether you have run the FOR`NEXT iterations to the end.
Example.
@You can tell if a natural number n is a prime number by dividing it by a natural number that does not exceed ãn. If you decide to make the divisors in order with FOR`NEXT and break through the iterations, you can determine whether there is a divisor by examining the value of the control variable where you exit FOR`NEXT.
100 INPUT n 110 FOR k=2 TO SQR(n) 120 IF MOD(n,k)=0 THEN EXIT FOR 130 NEXT k 140 IF k>SQR(n) THEN 150 PRINT "Prime" 160 ELSE 170 PRINT "Composite number with a devisor ";k 180 END IF 190 END
FOR v=inital_value TO limit STEP increment
(Series of sentences )
NEXT v
|
means
LET own1=limit LET own2=increment LET v=initial_value DO UNTIL (v - own1)*SGN(own2)>0 (Series of sentences ) LET v=v+own2 LOOP |
own1, own2 are variables that are reserved for each FOR`NEXT syntax at the time of translation, and are variables that do not appear on the program. There are no ways to operate them directly in the program.
Example.
In ISO(ANSI) standard BASIC, containing Minmal BASIC, 30 line of the following program is executed 11 times and this program outputs from 100 to 110 in turn.
10 LET n=100 20 FOR n=n TO n+10 30 PRINT n 40 NEXT n 50 END
ƒNote„
Note that own1 and own2 are not variables reserved for each run of FOR sentence. Making a recursive call to FOR`NEXT using the GOSUB sentence from within the NEXT loop will not work properly (even if the control variable is evacuated correctly).
@ In Ver.7.2.0 or lator, we have changed the specification to secure own1,own2 as a local variable in the internal procedure when FOR`NEXT are witten in internal procedures. To ensure consistency with JIS, we also have the option to always reserve it as a program unit variable.