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 program 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, producing an endless
number of lines saying Number 1.
If your program is in an infinite loop, contact the operator to cancel it. An authorized user can issue the
CEMT SET TASK PURGE command to halt an exec.
DO FOREVER Loops
Sometimes you might want to write an infinite loop purposely; for instance, in a program that reads records
from a file until it reaches the end of the file. You can use the EXIT instruction to end an infinite loop when
a condition is met, as in the following example. More about the EXIT instruction appears in section “EXIT
Instruction” on page 48.
Control Flow within a Program
40
CICS TS for VSE/ESA: REXX Guide