A variable that controls the number of times a loop repeats is called a control
variable. Unless you specify otherwise, the control variable increases by 1 each
time the loop repeats.
DO number=1TO5
SAY 'Loop' number
SAY 'Hello!'
END
SAY 'Dropped out of the loop when number reached' number
This example results in five lines of Hello! preceded by the number of the loop.
The number increases at the bottom of the loop and is tested at the top.
Loop 1
Hello!
Loop 2
Hello!
Loop 3
Hello!
Loop 4
Hello!
Loop 5
Hello!
Dropped out of the loop when number reached 6
You can change the increment of the control variable with the keyword BY as
follows:
DO number=1TO10BY2
SAY 'Loop' number
SAY 'Hello!'
END
SAY 'Dropped out of the loop when number reached' number
This example has results similar to the previous example except the loops are
numbered in increments of two.
Loop 1
Hello!
Loop 3
Hello!
Loop 5
Hello!
Loop 7
Hello!
Loop 9
Hello!
Dropped out of the loop when number reached 11
Infinite Loops
What happens when the control variable of a loop cannot attain the last number?
For example, in the following exec segment, count does not increase beyond 1.
DO count=1to10
SAY 'Number' count
count = count - 1
END
The result is called an infinite loop because count alternates between 1 and 0 and
an endless number of lines saying Number 1 appear on the screen.
Using Looping Instructions
48
z/OS V1R1.0 TSO/E REXX User’s Guide