10.2.1 FOR...NEXT Loops
In a FOR...NEXT loop, you specify a loop control variable
(the loop index) that determines the number of loop itera-
tions. This number must be a scalar (unsubscripted) variable.
When BASIC begins execution of a FOR...NEXT loop, the
starting and ending values of the loop control variable are
known.
The FOR statement assigns the control variable a starting
value and an ending value. You can use the optional STEP
clause to specify the amount to be added to the loop control
variable after each loop iteration.
When a FOR loop block executes, the BASIC compiler per-
forms the following steps:
1. Evaluates the starting value and assigns it to the control
variable.
2. Evaluates the ending value and the step value and assigns
these results to temporary storage locations.
3. Tests whether the ending value has been exceeded. If
the ending value has already been exceeded, BASIC exe-
cutes the statement following the NEXT statement. If the
ending value has not been exceeded, BASIC executes the
statements in the loop.
4. Adds the step value to the control variable and trans-
fers control to the FOR statement, which tests whether
the ending value has been exceeded. Steps 3 and 4 are
repeated until the ending value is exceeded.
Note that BASIC performs the test before the loop executes.
When the control variable exceeds the ending value, BASIC
exits the loop, and then subtracts the step value from the con-
trol variable. This means that after loop execution, the value
of the control variable is the value last used in the loop, not
the value that caused loop termination.
Example 10-1 assigns the values 1 to 10 to consecutive array
elements 1 to 10 of New_array , and Example 10-2 assigns
consecutive multiples of 2 to the odd-numbered elements of
New_array .
Note that the starting, ending, and step values can be run-
time expressions. You can have BASIC calculate these values
when the program runs, as opposed to using a constant value.
For instance, the following example assigns sales information
to array Sales_data . The number of iterations depends on
the value of the variable Days_in_month , which represents
the number of days in that particular month.
FOR I% = 1% TO Days_in_month
Sales_data(I%) = Quantity_sold
NEXT I%
Because the starting, ending, and step values can be numeric
expressions, they are not evaluated until the program runs.
This means that you can have a FOR...NEXT loop that does
not execute. The following example prompts the user for the
starting, ending, and step values for a loop, and then tries to
execute that loop. The loop executes zero times because it is
impossible to go from 0 to 5 using a step value of -1.
counter% = 0%
INPUT "Start"; start%
INPUT "Finish"; finish%
INPUT "Step value"; step_val%
FOR I% = start% TO finish% STEP step_val%
counter% = counter% + 1%
NEXT I%
PRINT "This loop executed"; counter%; "times."
Output
Start? 0
Finish? 5
Step value? -1
This loop executed 0 times.
Whenever possible, you should use integer variables to con-
trol the execution of FOR...NEXT loops because some decimal
fractions cannot be represented exactly in a binary com-
puter, and the calculation of floating-point control variables is
subject to this inherent imprecision.
In the following example, the first loop uses an integer con-
trol variable while the second uses a floating-point control
variable. The first loop executes 100 times and the second 99
times. After the ninety-ninth iteration of the second loop, the
internal representation of the value of Floating_point_variable
exceeds 10 and BASIC exits the loop. Because the first loop
uses integer values to control execution, BASIC does not exit
the loop until Integer_variable equals 100.
EXAMPLE: Click to display example.
Output
Integer loop count: 100
Integer loop end: 100
Real loop count: 99
Real loop end: 9.9
Although it is not recommended programming practice, you
can assign a value to a FOR...NEXT loop's control vari-
able while in the loop. This affects the number of times a
loop executes. For example, assigning a value that exceeds
the ending value of a loop will cause the loop's execution to
end as soon as BASIC performs the termination test in the
FOR statement. Assigning values to ending or step variables,
however, has no effect at all on the loop's execution.