For…To Command

Iterative loop. Code that follows the line containing a For…To command, until the matching Next command, is executed once with variable set to each value from expression1 to expression2 in increments of expression3.

Syntax

For variable = expression1 To expression2 [By expression3]

where

expression1 Defines beginning of the loop.
expression2 Defines end of the loop.
expression3 Defines the increment. Optional; if expression3 is not specified, the default value is 1.

Remarks

If expression1 is greater than expression2 and expression3 is not negative, the intervening code is not executed. When the loop begins, expression2 and expression3 are evaluated once, so changing the variables used in them inside the loop has no effect on the loop.

For compatibility with BASIC, Step may be used in place of By.

Example

DECLARE Test_Value

'This example will print 2, 3, 4, 5, 6, 7, 8, 9, 10.

FOR Test_Value = 2 TO 10

PRINT "Test value = ",Test_Value

NEXT Test_Value

'This example will print 2, 4, 6, 8, 10.

FOR Test_Value = 2 TO 10 BY 2

PRINT "Test value = ",Test_Value

NEXT Test_Value

'This example will print 5, 4, 3, 2, 1.

FOR Test_Value = 5 TO 1 BY -1

PRINT "Test value = ",Test_Value

NEXT Test_Value