HTBasic Help
×
Menu
Index

FOR ... NEXT

Executes a loop a fixed number of times.
 
 FOR control-var = start TO end [STEP step]
statements
NEXT control-var
 
Usage:
10  FOR I=1 TO 100
20    FOR X=1 TO 100
30      PRINT I,X
40    NEXT X
50    FOR J=2*PI TO 0 STEP -PI/100
. . .
80    NEXT J
90  NEXT I
 
Example:
 
Description:
The FOR ... NEXT loop is executed a fixed number of times, by incrementing a control variable through a fixed range. The loop consists of statements between the FOR and corresponding NEXT statement.
 
When the FOR statement is executed, the initial value is assigned to the control variable. The value is then tested against the final value. If it exceeds it (in the proper STEP direction) then the FOR loop is not executed and control transfers to the line following the matching NEXT statement. If there is no STEP modifier, the default step size is set to one. The step modifier can be positive or negative. If the step modifier is zero, then the loop is infinitely repeated and no error is generated.
 
When the NEXT statement is executed, the step value is added to the control variable. If the new control value variable is larger than the end value and the step value is positive (or if the new control variable value is smaller than the end value and the step value is negative), the loop terminates and execution continues with the statement following the NEXT. If the control variable has not exceeded the end value, then control is returned to the program statement following the corresponding FOR statement.
 
Jumping from outside the FOR loop into the FOR loop does not give an error but should not be done since the control variable, end value and step value will not be properly set. Jumping from inside the FOR loop to outside the FOR loop is permitted.
 
See Also: