Runs a block of statements for all numbers 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. It declares an integer variable.
- Optional in the line that starts with
- Optional data type of
counter. Permitted types include:
- Mandatory integer expression.
It becomes the initial value of
- Mandatory integer expression.
It can become the last value of
counterbefore the loop stops.
- Optional integer expression.
It is the increment for
counter. The default is one (
- Optional One or more statements between the lines that start with
End. They run for each value between
- The alternative is two or more blocks of statements.
The initial group starts with
Exit When, and runs the same as specified above. The subsequent groups start with
When, and run when the group after
Exit Whencannot 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
You can use
For when you want to run a sequence of statements again and again.
It is recommended when you can be sure how many times the statements must run.
Control is connected with the variable
counter specified in the line that starts with
Other loop Constructs are possibly more flexible.
See the See also section below for more information.
When a loop of type
For starts, ViviFire calculates
ViviFire calculates these values only one time and gives
counter the value of
Before the statement block runs, ViviFire compares
counter is larger than
end_value (or smaller if
step_value is negative), the loop does not run, then control moves to the statement after
But the opposite result usually occurs, thus the loop runs.
Each time ViviFire runs
End, it adds
counter and goes back to
Again it compares counter to end_value, and again it runs the block or stops the loop as a result.
This process continues until
counter becomes out-of-range of
Alternatively, one of the statements
Return can stop the loop.
step_value can be positive or negative.
The value of this parameter selects how the loop runs as follows:
|Value||Loop runs if|
|Positive or zero|
If you do not supply
step_value, its default value is 1.
- The variable
counteris always locally scoped to the loop body.
- This variable cannot have the same name as a different (unqualified) variable that is available in the same scope.
- The variable must have an integer data type.
- You are not permitted to change the value of the variable directly.
Exit For can stop this type of loop.
Exit For immediately moves control to the statement after
Exit can also stop more than one nested loop.
You can use the statement «
Exit For, For» in the inner loop to stop two loops.
To find if a loop of this type completed because
counter became more than
end_value, use «
To find if a loop of this type did not run because
counter was initially out-of-range, use «
See Exit When Clause for more information.
For index As Int32 = 1 To 5 #Debug.Print index End For #Debug.PrintLine