FOR...NEXT Construct

The FOR...NEXT construct is designed to allow a set of statements to be repeated and a variable to be incremented until the variable reaches a designated maximum value.

Format

FOR var = start-expr TO end-expr [STEP step-expr]   [WHILE | UNTIL exprstatements

NEXT var

Parameter(s)

var=start-expr

Sets the specified variable var to the value of start-expr for the first iteration.

TO end-expr

Stops iterating when var exceeds end-expr.

STEP step-expr

Increments var by step-expr for each iteration. If the STEP step-expr clause is not specified, 1 is the default increment.

WHILE expr

Continues the loop as long as expr evaluates to true. Once expr evaluates to false, continues program execution after the NEXT statement.

UNTIL expr

Continues the loop until expr evaluates to true. Once expr evaluates to true, continues program execution after the NEXT statement.

Description

A FOR...NEXT program loop is a series of statements that execute repeatedly, with a variable var incremented by a specified amount for each iteration. (The default increment is 1.) The first time the statements are executed, the specified variable var is assigned the value start-expr. The second time they are executed, var is assigned the value start-expr +1 and so on, until var exceeds the value of end-expr. The NEXT clause defines the end of the loop, forces the increment of the variable var, and returns control to the FOR statement. The body of the loop is skipped if start-expr is greater than the end-expr and step-expr is positive.

At the beginning of each iteration, a check is performed on the value of the counter var. If it is less than or equal to the end value end-expr, program execution branches to the statement following the FOR statement and the process is executed. If it is greater than the end value, execution continues with the statement following the NEXT statement. Note that although start-expr is usually 1 and end-expr is usually specified as a positive integer, these are not true restrictions: any mvBASIC expression which evaluates to a numeric value may be used.

Alternative syntax forms involve the STEP clause and the WHILE and UNTIL clauses.

If the STEP step-expr clause is specified, step-expr is taken as the increment instead of 1, and the value of var is increased by step-expr for each iteration. Note that step-expr does not have to be positive, so var can exceed end-expr on the onset of a loop with a negative step, and the loop continues as long as var is greater than or equal to end-expr. In addition, step-expr does not have to evaluate to an integer, so any decimal value can be specified as the step in a FOR loop.

The WHILE and UNTIL clauses can be added to the FOR loop to provide additional flexibility. These clauses behave just as they do in the standard LOOP structure. See LOOP Construct for more information.

FOR...NEXT loops may be nested, providing that each loop has a unique variable var as its counter, and the NEXT statement for the inside loop appears before the NEXT statement for the outside loop.

It may be helpful to think of the function of a FOR...NEXT loop as logically identical to:

var = start-expr

LOOP

WHILE var <= end-expr  DO

[ statements ]

var += step-expr

REPEAT

Example

In this application the DCOUNT function returns the number of attributes in the dynamic array CUST.REC, and a FOR loop is used to print out each attribute of the array.

NO.OF.ATTRS = DCOUNT(CUST.REC,CHAR(254))

FOR I = 1 TO NO.OF.ATTRS

   PRINT I,CUST.REC < I >

NEXT I

See Also

Statement and Function Reference