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
, andUInt128
. 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 added to
counter
after each time through the loop. The default is one (1
). statements
- Optional one or more statements between
For
andEnd
. They run for each value betweenstart_value
andend_value
. - The alternative is two or more groups of statements.
The initial group starts with
Exit When
, and runs the same as given above. The subsequent groups start withWhen
, and run when (1) the loop cannot start, or (2) after the loop stops. See § When statements for more information. End
- Completes the statement.
You can also use
End For
. The optional variablecounter
can only followEnd For
. - You can change the syntax of this part. See § Changes in syntax with @Option for more information.
Instructions
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 § See also below for more information.
In the example that follows, the variable index
starts with a Value of one (1).
Its Value increases by one each time through the loop.
The loop stops after the Value of index
gets to 5.
For index = 1 To 5
#Debug.Print $"{index} "
End For
#Debug.PrintLine
' Output: 1 2 3 4 5
The next example simulates the countdown for the launch of a spacecraft.
It starts at 10 and goes down to one, with a one second pause after each iteration.
The clause Step -1
causes the Value of countdown
to decrease by one.
For countdown = 10 To 1 Step -1
#Debug.PrintLine countdown
Sleep 1000 ' milliseconds
End For
#Debug.PrintLine "BLASTOFF!"
Usual operation
When this type of loop starts, ViviFire calculates start_value
, end_value
, and step_value
.
ViviFire calculates these values only one time and assigns counter
the value of start_value
.
Before the statement block runs, ViviFire compares counter
to end_value
.
If it is less than or equal to end_value
and step_value is greater than zero, the loop runs.
And if it is greater than or equal to end_value
and step_value is less than zero, the loop runs.
But if the two conditions are false, the loop does not run and control moves to the statement after End
.
Each time ViviFire gets to 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 expression selects how the loop runs as follows:
Value | What the loop does |
---|---|
Greater than zero | Runs while counter <= end_value |
Less than zero | Runs while counter >= end_value |
Zero | Raises the exception RangeError |
If you do not supply step_value
, its default value is 1.
Counter
- The variable
counter
is 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
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.
Changes in syntax with @Option
There are two areas where you can chage the syntax of the statement:
@Option End Block
and@Option End
@Option End For ID
and@Option End For
See @Option Directive for more information.
Examples
TODO
See also
- Doo...Loop Statement – Runs because of a pre-condition or a post-condition.
- For Each Statement – Runs through a given data structure.
- While Statement – Runs because of a pre-condition.
- Exit Statement
- Exit When Clause
- Loop Constructs
- @Option Directive