loop

The loop statement executes one or more statements multiple times.

Syntax

looplabel [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:

  1. The number of loops specified by the count parameter are completed

  2. At least as many milliseconds as are specified by the timelimit parameter elapse

  3. 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

loop example 10 {
    This is loop number ${example.COUNT}
}

The loop shown above will result in the following output:

This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
This is loop number 5
This is loop number 6
This is loop number 7
This is loop number 8
This is loop number 9
This is loop number 10

Last updated