Caché MultiValue Basic Reference
FOR...NEXT
|
|
Repeats a group of statements a specified number of times.
Synopsis
FOR var = start TO end
[STEP increment]
[WHILE expression]
[UNTIL expression]
statements
NEXT [var]
The
FOR...NEXT statement begins with a FOR keyword with
var=
start TO
end to establish a loop counter. This is followed by one or more optional clauses: STEP, WHILE, and UNTIL. The loop itself consists on one or more executable
statements. The FOR loop is ended by the mandatory NEXT keyword.
The counter functions as follows:
-
If
start <
end, the loop executes the specified number of times.
-
-
If
start >
end, the loop does not execute.
Most commonly,
start and
end are positive integers. They can, however, be positive or negative integers or decimal numbers.
The optional STEP clause sets an increment (or decrement) for the counter. By default, the counter increments by 1. The
increment argument can be either positive (increment) or negative (decrement). Most commonly
increment is an integer, but it can be a decimal number. An
increment of 0 causes an infinite loop.
Once the loop starts and all statements in the loop have executed,
increment is added to the counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the NEXT keyword.
You can nest
FOR...NEXT loops by placing one
FOR...NEXT loop within another. Give each loop a unique variable name as its counter. The following construction is correct:
FOR i = 1 TO 10
FOR j = 1 TO 10
FOR k = 1 TO 10
! Some statements
NEXT
NEXT
NEXT
You can use a
CONTINUE statement to interrupt a loop and return to the counter.
Changing the value of counter while inside a loop can make it more difficult to read and debug your code.
Caché MVBasic permits you to exit or enter a
FOR loop using a
GOTO statement. This implementation of
GOTO follows MultiValue standards, and is less restrictive than the ObjectScript standard for
GOTO statements.
Caché MVBasic supports FOR.INCR.BEF as the Caché default. This option increments the
FOR loop counter before performing bounds checking. To perform bounds checking before incrementing the loop, specify
$OPTIONS FOR.INCR.BEF to turn off this option.