loop
The loop
statement executes one or more statements multiple times.
Syntax
loop
label [count]
[timeout
timelimit]
{
# Statements
}
The opening {
may be placed on a line of its own if preferred but the closing }
must be on a line of its own
Details
The loop
statement will loop indefinitely unless one of three exit conditions cause it to stop. These are as follows:
The number of loops specified by the count parameter are completed
At least as many milliseconds as are specified by the timelimit parameter elapse
An exit_loop statement explicitly exits the loop
In all three cases when the loop exits, execution of the script will continue from the first statement after the closing }
marking the end of the loop.
In the event that both count and timelimit parameters are specified, the loop will exit as soon as one or other of the limits have been reached, whichever comes first.
Both the count and timeout parameters are optional. If omitted then the default for both of them will be infinite.
The loop
statement will automatically create and update a variable called loop_label.COUNT
which can be referenced to determine how many times the loop has executed (as shown in the example below). This variable is not deleted when the loop exits which means that it is possible to know how many times any given loop executed, even after the loop has exited.
Any specified timeout value is evaluated at the end of each execution of the loop and as such the actual time before the loop exits is likely to be a short time (typically a few milliseconds) greater than the specified value. In practice this should be of no consequence.
Example
The loop shown above will result in the following output:
Last updated