For Statement

Executes a sequence of statements for all values in a range.

Syntax

For counter [ As type ] = start_value To end_value [ Step step_value ]
    [ statements ]
End [ For [ counter ] ]

Parts

counter
Mandatory in the line that starts with For – a numeric variable.
Optional in the line that starts with End
type
Optional data type of counter. Permitted types include: Int8, Int16, Int32, Int, Int64, Int128, UInt8, UInt16, UInt32, UInt, UInt64, and UInt128.
start_value
Mandatory integer expression – the initial value of counter.
end_value
Mandatory integer expression – the last value of counter.
step_value
Optional integer expression – the increment for counter. The default is one (1).
statements
Optional One or more executable statements between the lines that start with For and End. They execute for each value between start_value and (end_value.
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 Begin cannot start, or after it completes. See the When statements section for more information.
End
Completes the statement. You can also use End For. The optional variable counter can only follow End For.
You can change the syntax of this part. See @Option Directive for more information.

Instructions

End For is not the same as Exit For.

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 For. Other loop structures are possibly more flexible. See the See also section below for more information.

Usual execution

When a loop of type For starts, ViviFire evaluates start_value, end_value, and step_value. ViviFire evaluates these values only one time and gives counter the value of start_value. Before the statement block executes, ViviFire compares counter to end_value. If 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 End. But the opposite result is more typical, thus the loop executes.

Each time ViviFire executes End, it adds step_value to counter and goes back to For. 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 Exit executes.

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

The statement Exit For can stop such loops. Exit For immediately moves control to the statement after End For.

When statements

To find if a loop of type For completed because counter became more than end, use When Done.

To find if a loop of type For did not execute because counter was initially out-of-range, use When None.

See Begin…When Statements for more information.

Examples

For index As Int = 1 To 5
    #Debug.Print index
End For
#Debug.PrintLine

See also