Repetitive DO Loops
If a DO instruction has a repetitor phrase or a conditional phrase or both, the group of instructions forms a
repetitive DO loop. The instructions are processed according to the repetitor phrase, optionally modified
by the conditional phrase. (See section “Conditional Phrases (WHILE and UNTIL)” on page 140).
Simple Repetitive Loops: A simple repetitive loop is a repetitive DO 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
nominally processed “forever”, that is, until the condition is satisfied or a REXX instruction is processed
that ends the loop (for example, LEAVE).
Note: For a discussion on conditional phrases, see section “Conditional Phrases (WHILE and UNTIL)” on
page 140.
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 */
Do 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 name,acontrol variable that is assigned
an initial value (the result of expri, formatted as though 0had 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 while the end condition (determined by the result of exprt)is
not met. If exprb is positive or 0, the loop is ended when name is greater than exprt. If negative, the loop
is ended when name is less than exprt.
The expri,exprt, and exprb options must result in numbers. They are evaluated only one time, 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 indefinitely unless some other condition stops it.
Example:
Do 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 */
DO
Chapter 13. Keyword Instructions 139