Repetitive Loops

The simplest loop tells the language processor to repeat a group of instructions a specific number of times. It uses a constant after the keyword DO.
DO 5
SAY 'Hello!'
END
When you run this example, it produces five lines of Hello! :
Hello!
Hello!
Hello!
Hello!
Hello!
You can also use a variable in place of a constant, as in the following example, which gives you the same results.
number = 5
DO number
SAY 'Hello!'
END
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 = 1 TO 5
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 = 1 TO 10 BY 2
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 = 1 to 10
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.
Figure 1. Example Using a DO FOREVER Loop
/******************************* REXX
********************************/
/* This program processes strings until the value of a string is */
/* a null string. */
/*********************************************************************/
DO FOREVER
PULL string /* Gets string from input stream */
IF string = '' THEN
PULL file_name
IF file_name = '' THEN
EXIT
ELSE
DO
result = process(string) /* Calls a user-written function */
/* to do processing on string. */
IF result = 0 THEN SAY "Processing complete for string:" string
ELSE SAY "Processing failed for string:" string
END
END

This example sends strings to a user-written function for processing and then issues a message that the processing completed successfully or failed. When the input string is a blank, the loop ends and so does the program . You can also end the loop without ending the program by using the LEAVE instruction. The following topic describes this.

LEAVE instruction

The LEAVE instruction causes an immediate exit from a repetitive loop. Control goes to the instruction following the END keyword of the loop. An example of using the LEAVE instruction follows:
Figure 2. Example Using the LEAVE Instruction
						/******************************** REXX
						*******************************/
						/* This
						program
						uses the LEAVE instruction to exit from a DO */
						/* FOREVER loop. */
						/*********************************************************************/
						DO FOREVER
						PULL string /* Gets string from input stream */
						IF string = 'QUIT' then
						LEAVE
						ELSE
						DO
						result = process(string) /* Calls a user-written function */
						/* to do processing on string. */
						IF result = 0 THEN SAY "Processing complete for string:" string
						ELSE SAY "Processing failed for string:" string
						END
						END
						SAY 'Program run complete.'
					

ITERATE instruction

The ITERATE instruction stops execution from within the loop and passes control to the DO instruction at the top of the loop. Depending on the type of DO instruction, the language processor increases and tests a control variable or tests a condition to determine whether to repeat the loop. Like LEAVE, ITERATE is used within the loop.
DO count = 1 TO 10
IF count = 8
THEN
ITERATE
ELSE
SAY 'Number' count
END
This example results in a list of numbers from 1 to 10 with the exception of number 8.
Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 9
Number 10

Exercises: using loops

What are the results of the following loops?
DO digit = 1 TO 3
SAY digit
END
SAY 'Digit is now' digit
DO count = 10 BY -2 TO 6
SAY count
END
SAY 'Count is now' count
DO index = 10 TO 8
SAY 'Hup! Hup! Hup!'
END
SAY 'Index is now' index
Sometimes an infinite loop can occur when input to end the loop does not match what is expected. For instance, in the example of using the LEAVE instruction, what happens when the input is Quit and a PARSE PULL instruction replaces the PULL instruction?
PARSE PULL file_name
ANSWERS
  1. The results of the repetitive loops are as follows:
    1. 1
      2
      3
      Digit is now 4

    2. 10
      8
      6
      Count is now 4

    3. Index is now 10

  2. The program would be unable to leave the loop because Quit is not equal to QUIT . In this case, omitting the PARSE keyword is preferred because regardless of whether the input is quit , QUIT , or Quit , the language processor translates the input to uppercase before comparing it to QUIT.


rvse023.html | Timestamp icon Last updated: Monday, 5 June 2017