FOR ... NEXT

Purpose To execute a series of instructions for a specified number of times in a loop
Syntax

FOR variable = x TO y [STEP z]
. . . .

NEXT

where variable may be any integer variable A to Z only and is used as a counter. x, y and z are numeric expressions. STEP z is an optional part of the statement.

x is the initial value of the counter, y is the final value of the counter. Program lines following the FOR statement are executed until the NEXT statement is encountered. Then the counter is incremented by the amount specified by STEP. If STEP is not specified, the increment is assumed to be 1.

A check is performed to see if the value of the counter is greater than the final value y if STEP is positive (or smaller than the y if STEP is negative). If it is not greater, the program branches back to the statement after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement. This is called a FOR-NEXT loop.

A run-time error will result if STEP is evaluated to be 0.

Examples

FOR I=1 TO 10
  FOR J = 100 to 1 STEP -10
    DM[I] = DM[J]
  NEXT

NEXT

Comments: FOR-NEXT loops may be nested; i.e. a FOR-NEXT loop may be placed within the context of another FOR-NEXT loop. When loops are nested, each loop must have a unique variable name as its counter. The NEXT statement for the inside loop must appear before that for the outside loop. Each Loop must have a separate NEXT statement to mark the end of the loop
See Also WHILE ... ENDWHILE

backbutton.gif (507 bytes)  Basic to TBASIC Reference Manual