Repetitive Loops

If a DO or LOOP instruction has a repetitor phrase, a conditional phrase, or both, the group of instructions forms a repetitive loop. The instructions are processed according to the repetitor phrase, optionally modified by the conditional phrase. (See Conditional Phrases (WHILE and UNTIL).)

Simple Repetitive Loops

A simple repetitive loop is a repetitive loop in which the repetitor phrase is an expression that evaluates to a count of the iterations.

If repetitor is omitted but there is a conditional or if the repetitor is FOREVER, the group of instructions is processed until the condition is satisfied or a Rexx instruction ends the loop (for example, LEAVE).

In the simple form of a repetitive loop, exprr is evaluated immediately (and must result in a positive whole number or zero), and the loop is then processed that many times.

Example:

/* This displays "Hello" five times */
Loop 5
say "Hello"
end

Note that, similar to the distinction between a command and an assignment, if the first token of exprr is a symbol and the second token is (or starts with) =, the controlled form of repetitor is expected.

Controlled Repetitive Loops

The controlled form specifies control1, a control variable that is assigned an initial value (the result of expri, formatted as though 0 had been added) before the first execution of the instruction list. The variable is then stepped by adding the result of exprb before the second and subsequent times that the instruction list is processed.

The instruction list is processed repeatedly as long as the end condition (determined by the result of exprt) is not met. If exprb is positive or 0, the loop is ended when control1 is greater than exprt. If negative, the loop is ended when control1 is less than exprt.

The expri, exprt, and exprb options must result in numbers. They are evaluated only once, before the loop begins and before the control variable is set to its initial value. The default value for exprb is 1. If exprt is omitted, the loop runs infinitely unless some other condition stops it.

Example:

Loop I=3 to -2 by -1      /* Displays:   */
  say i                   /*      3      */
end                       /*      2      */
                          /*      1      */
                          /*      0      */
                          /*     -1      */
                          /*     -2      */

The numbers do not have to be whole numbers:

Example:

I=0.3                     /* Displays:   */
Do Y=I to I+4 by 0.7      /*     0.3     */
  say Y                   /*     1.0     */
end                       /*     1.7     */
                          /*     2.4     */
                          /*     3.1     */
                          /*     3.8     */

The control variable can be altered within the loop, and this can affect the iteration of the loop. Altering the value of the control variable is not considered good programming practice, though it can be appropriate in certain circumstances.

Note that the end condition is tested at the start of each iteration (and after the control variable is stepped, on the second and subsequent iterations). Therefore, if the end condition is met immediately, the group of instructions can be skipped entirely. Note also that the control variable is referred to by name. If, for example, the compound name A.I is used for the control variable, altering I within the loop causes a change in the control variable.

The execution of a controlled loop can be limited further by a FOR phrase. In this case, you must specify exprf, and it must evaluate to a positive whole number or zero. This acts like the repetition count in a simple repetitive loop, and sets a limit to the number of iterations around the loop if no other condition stops it. Like the TO and BY expressions, it is evaluated only once--when the instruction is first processed and before the control variable receives its initial value. Like the TO condition, the FOR condition is checked at the start of each iteration.

Example:

Loop Y=0.3 to 4.3 by 0.7 for 3  /* Displays:    */
  say Y                         /*     0.3      */
end                             /*     1.0      */
                                /*     1.7      */

In a controlled loop, the control1 name describing the control variable can be specified on the END clause. This name must match control1 in the DO or LOOP clause in all respects except the case (note that no substitution for compound variables is carried out). Otherwise, a syntax error results. This enables the nesting of loops to be checked automatically, with minimal overhead.

Example:

Loop K=1 to 10
...
...
End k  /* Checks that this is the END for K loop */

Note: The NUMERIC settings can affect the successive values of the control variable because Rexx arithmetic rules apply to the computation of stepping the control variable.