A repeat count

You can specify how many times to repeat the instructions within the loop by putting a numeric expression after the DO keyword. For example, here we say "Hello" 10 times.

/* Say "Hello" ten times */
DO 10
   SAY "Hello"
END

Alternately, the numeric expression could be represented by a variable's value (or any mathematical expression).

/* Say "Hello" ten times */
My_Variable = 10
DO My_Variable
   SAY "Hello"
END

Note: Even if you were to change the value of My_Variable within the loop (ie, put an assignment instruction inside of the loop to set My_Variable to a new value), the loop would still execute 10 times. This usage of DO copies the initial value of My_Variable to an internal REXX variable which is then used to count how many times the loop repeats.


Looping endlessly

If instead of a numeric expression, you place the keyword FOREVER after DO, then the loop will continue to repeat indefinitely (unless the user presses the CTRL and C keys to abort the script, or the ITERATE or LEAVE keywords are used within the loop).

/* This script keeps saying "Hello" indefinitely */
DO FOREVER
   SAY "Hello"
END

Loop counters

Control loops use a variable (called the "control variable") as a counter. You specify the range over which the variable's (numeric) value is to be incremented, by specifying the initial (ie, start) value of the variable as well as the end value (ie, at what value the loop will stop repeating). REXX automatically increments the variable's value upon each repeat of the loop, terminating the loop when the variable's value reaches the end value. (REXX also initializes the variable to the start value upon the first iteration of the loop). By default, the control variable increments by 1. The start value is specified after the = sign following the variable name. The end value is specified after the TO keyword. The end value must be greater than or equal to the start value in order for the loop to be executed. (If the start value is equal to the end value, then the loop is executed once only). If the end value is less than the start value, then the loop will be skipped. You may use the variable in some instruction within the loop, and this is extremely useful when you need to step through some array, for example.

/* Count from 1 to 20, inclusive, saying
   each incremented value of the variable */
DO My_Variable=1 TO 20
   SAY My_Variable
END

Here we print out the values of the array elements Array.1, Array.2, Array.3, and Array.4.

/* Print values of array elements
   Array.1, Array.2, Array.3 and Array.4 */
DO My_Variable=1 TO 4
   SAY Array.My_Variable
END

You can use a variable's value (or any mathematical expression) to specify the start or end values. Here we use the value of the variable count to specify the end value.

/* Count from 1 to the value of count, inclusive,
   saying each incremented value of the variable */
count = 4
DO My_Variable=1 TO count
   SAY My_Variable
END

You can specify that the variable's value is to be incremented in steps other than by 1. Use the BY keyword, followed by a numeric expression (ie, could be a variable or mathematical expression too) representing the increment amount. For example, here My_Variable is incremented by 2.3 upon each repeat of the loop.

/* Print all multiples of 2.3 not more than 20 */
DO My_Variable=0 TO 20 BY 2.3
   SAY My_Variable
END

You can even setup a loop such that the variable's value is decremented. When doing this, the start value should be higher than the end value, and you should specify a negative increment amount (ie, a decrement amount). Here we count down from 20 to 1 (ie, decrementing by 1 upon each repeat of the loop).

/* Count from 20 to 1, inclusive,
   saying each decremented value of the variable */
DO My_Variable=20 TO 1 BY -1
   SAY My_Variable
END

In fact, you could also use negative values for the start and end values.

/* Count from -5 to -1, inclusive,
 saying each incremented value of the variable */
DO My_Variable=-5 TO -1
   SAY My_Variable
END

Instead of specifying a start and end value, you can specify a start value and, using the FOR keyword, specify how many times the loop is to repeat. The repeat count is the numeric expression after FOR. Again, this can also be specified using a variable's value (or a mathematical expression).

/* Print the first five multiples
   of 5.7 (ie, 0, 5.7, 11.4, 17.1, 22.8) */
DO My_Variable=0 FOR 5 BY 5.7
   SAY My_Variable
END

If you only specify a start value, but no end value or repeat count, then the loop will repeat forever, incrementing the variable's value by the increment amount (unless the user presses CTRL and C keys, or the ITERATE or LEAVE keywords are used within the loop).

/* Print all the natural numbers */
DO My_Variable=0
   SAY My_Variable
END My_Variable

The My_Variable at the end of the preceding example is optional. At the end of any control loop, the name of the control variable may be placed after the END keyword, where it will be checked to see if it matches the control variable.

Note: Changing the value of My_Variable within any of the above control loops (ie, put an assignment instruction inside of the loop to set My_Variable to a new value), will affect how many times the loop repeats. This usage of DO checks the value of My_Variable each time the loop repeats, incrementing its current value accordingly. In fact, after the loop has finished, the value of My_Variable will then be one more than the repeat count.