Executes a sequence of statements for all values in a range.
For counter [ As type ] = start_value To end_value [ Step step_value ] [ statements ] End [ For [ counter ] ]
- Mandatory in the line that starts with
For– a numeric variable.
- Optional in the line that starts with
- Optional data type of
counter. Permitted types include:
- Mandatory integer expression – the initial value of
- Mandatory integer expression – the last value of
- Optional integer expression – the increment for
counter. The default is one (1).
- Optional One or more executable statements between the lines that start with
End. They execute for each value between
- The alternative is two or more groups of statements.
The initial group starts with
Begin, and executes the same as specified above. The subsequent groups start with
When, and execute when the group after
Begincannot start, or after it completes. See the When statements section for more information.
- Completes the statement.
You can also use
End For. The optional variable
countercan only follow
- You can change the syntax of this part. See @Option Directive for more information.
End For is not the same as
Use the construct
For when you must execute one or more statements again and again.
It is recommended when you can be sure how many times the statements must execute.
Control is connected with the variable
counter specified in the line that starts with
Other loop structures are possibly more flexible.
See the See also section below for more information.
When a loop of type
For starts, ViviFire evaluates
ViviFire evaluates these values only one time and gives
counter the value of
Before the statement block executes, ViviFire compares
counter is larger than
end_value (or smaller if
step_value is negative), then the loop does not execute and execution moves to the statement after
But the opposite result is more typical, thus the loop executes.
Each time ViviFire executes
End, it adds
counter and goes back to
Again it compares counter to end_value, and again it executes the block or stops the loop as a result.
This process continues until
counter becomes larger than
end_value or the statement
If the value of
step_value is zero, then an error occurs.
A constant or literal of zero causes a compiler error.
A variable with a value of zero will cause a run-time error.
Exit For can stop such loops.
Exit For immediately moves control to the statement after
To find if a loop of type
For completed because
counter became more than
To find if a loop of type
For did not execute because
counter was initially out-of-range, use
See Begin…When Statements for more information.
For index As Int = 1 To 5 #Debug.Print index End For #Debug.PrintLine