Skip to main content

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]

Arguments

The FOR...NEXT statement syntax has these parts:

var A numeric variable used as a loop counter. var must be a local variable. It can be a % variable. It can be a subscripted array. var cannot be an @ variable, a global variable, or an object property. It cannot be an element of a user-defined type.
start Initial value of counter.
end Final value of counter.
STEP increment Optional — The STEP clause sets the amount the counter is changed each time through the loop. increment can be a positive or negative integer. If a STEP clause is not specified, increment defaults to 1. If increment is 0, FOR...NEXT loops infinitely.
WHILE expression UNTIL expression Optional — The WHILE and UNTIL clauses specify a test condition for exiting the FOR loop. You can omit or specify either clause, or specify both clauses in any order.
statements One or more statements between FOR and NEXT that are executed the specified number of times.

Description

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 executes once.

  • 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.

Notes

Changing the value of counter while inside a loop can make it more difficult to read and debug your code.

FOR and GOTO

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.

FOR.INCR.BEF

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.

See Also

FeedbackOpens in a new tab