For Statement

Runs a block of statements for all numbers 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. It declares an integer variable.
Optional in the line that starts with End
type
Optional data type of counter. Permitted types include: Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, and UInt128.
start_value
Mandatory integer expression. It becomes the initial value of counter.
end_value
Mandatory integer expression. It can become the last value of counter before the loop stops.
step_value
Optional integer expression. It is the increment for counter. The default is one (1).
statements
Optional One or more statements between the lines that start with For and End. They run for each value between start_value and (end_value.
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 When 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.

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

Usual operation

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

Each time ViviFire runs End, it adds step_value to counter and goes back to For. 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 end_value. Alternatively, one of the statements Exit or Return can stop the loop.

step

step_value can be positive or negative. The value of this parameter selects how the loop runs as follows:

Step
Value Loop runs if
Positive or zero counter <= end_value
Negative counter >= end_value

If you do not supply step_value, its default value is 1.

Counter

Exit For

The statement Exit For can stop this type of loop. Exit For immediately moves control to the statement after End.

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.

When statements

To find if a loop of this type completed because counter became more than end_value, use «When DONE». To find if a loop of this type did not run because counter was initially out-of-range, use «When NONE». See Exit When Clause for more information.

Examples

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

See also