repeat for

control structure

Syntax
repeat (for integer name)? 
   (from numeric expression)?
   (to numeric expression)?
   (by numeric expression)?
actions
again


Purpose

You can use a repeat for loop to repeat a loop a certain number of times. A repeat for loop has a control variable which has a different value for each iteration of the loop. You can specify the start and end values of the control variable and the steps by which it will be incremented or decremented each time. All these parameters can be omitted, and the appropriate defaults will be applied.

Here is a simple repeat for that repeats 10 times:

  process
     repeat for integer i from 1 to 10
        output "d" % i || " "
     again

Since the default value of the from clause is 1, you can omit the from clause from the program above:

  process
     repeat for integer i to 10
        output "d" % i || " "
     again

You can use the by clause to specify the amount by which the control value is incremented:

  process
     repeat for integer i to 100 by 10
        output "d" % i || " "
     again

The above program prints out:

  1 11 21 31 41 51 61 71 81 91

Notice that the control variable does not always reach the value of the to clause. The loop exits if the next value of the control variable would exceed the value of the to clause.

To make the above program count from 10 to 100, you would need to add a from clause:

  process
     repeat for integer i from 10 to 100 by 10
        output "d" % i || " "
     again

You can change the direction of the loop's progress by specifying a negative value for the by clause.

  process
     repeat for integer i from 100 to 10 by -10
        output "d" % i || " "
     again

A repeat for loop will exit under any of the following conditions:

  • a throw is executed inside the loop and is not caught inside the loop
  • an exit statement is executed
  • the value of the control variable exceeds the value of the to clause, or is less than the value of the to clause when the by value is negative
  • the value of the control variable exceeds the maximum value of the integer (or the minimum value when the by value is negative).

If you omit any of the from, to, and by values, the default values will be used. The defaults are:

  • from: 1
  • to: the maximum value for an integer
  • by: 1

In all other respects, a repeat for loop behaves like a regular repeat loop.