Kilowatt Software L.L.C.
Classic Rexx Tutorial
Language Level 4.00 (TRL-2)

Do instruction group

The do instruction group is a complicated set of concepts to learn. These concepts are presented as the following topics.

A do instruction group can be prone to logic pitfalls, when either the do instruction or corresponding end clause is erroneously absent. The RFLOW program that is provided with this tutorial can help you study, and correct, the control flow of your REXX programs.


Simple do instruction group

A do instruction without any additional clauses is used for grouping a sequence of instructions. This form of do instruction is used when more than one instruction is performed after the then keyword of an if instruction, or when clause within a select instruction group.

A name is not permitted with the end instruction, when it concludes a simple do instruction group.


Iterative do loops

An iterative Do instruction group has the following syntax variations.

Rexx provides a rich set of looping capabilities. An iterative do instruction can have a repetitorClause and/or a conditionalClause. When both of these clauses are absent, it is a simple do group instead.

Looping capabilities can be further refined by using leave and iterate instructions.


Simple repetitive loops

There are two types of simple repetitive loops. One is repeated forever, the other is repeated a finite number of times. You will rarely want to repeat a loop an infinite number of times. Instead, you will probably add a leave instruction. However, there are some Rexx programs that you will want to run indefinitely until they are cancelled.

Here are examples of simple repetitive loops.

The repetition expression in a simple loop might contain an equal sign. This might be misconstrued as a counted loop, which is discussed in the next section. When you want a simple repeated loop you should enclose the expression in parentheses as follows:

If the parentheses around the count = linein() phrase were absent, the program would run forever. The count would begin with the number you entered, and would be incremented by 1 every loop cycle.


Counted loops

A counted loop has the following syntax.

Counted loops are commonly used in Rexx programs. Here is a simple example that shows the powers of two, from 1 to 32.

Here is another example, that shows every 4th power of two.

Here is another example, that shows every 4th power of two, but only 4 times.

Here is another example, that shows every 4th power of two, 8 times.

In the examples above explicit numbers were used for the initial value, limit value, step value, and the repetition value. Each of these values could have been an expression instead. Furthermore, the values do not have to be whole numbers, or positive. You can even step downwards instead of upwards. Here is an example where other value types are used.

Here is another example of a counted loop with a while phrase. This loop repeats indefinitely while more lines are available from the default input stream.

In the above example, the lineNumber is initially set to 1, and is incremented by 1 every loop cycle.


Conditional loops

A conditional loop has the following syntax. As described earlier, you can also have other iterative do phrases in front of the while or until keywords.

Here is a conditional loop that converts input lines to upper case.

Here is a conditional loop that shows line numbers in front of each input line.


End instruction considerations

A name can be specified with the end instruction, only when the associated do instruction has a named control variable.

When multiple do or select instruction groups are pending, the end instruction binds to the nearest incomplete instruction group. Consider the following example:

When multiple iterative do groups are pending, and each of these has a named control variable, then you should specify a name after each of the corresponding end instructions. If an inaccurate control variable is referenced, error 10 (unexpected or unmatched end is raised). For example: