Repetitive Loops
DO 5
SAY 'Hello!'
END
Hello!
Hello!
Hello!
Hello!
Hello!
number = 5
DO number
SAY 'Hello!'
END
DO number = 1 TO 5
SAY 'Loop' number
SAY 'Hello!'
END
SAY 'Dropped out of the loop when number reached' number
Loop 1
Hello!
Loop 2
Hello!
Loop 3
Hello!
Loop 4
Hello!
Loop 5
Hello!
Dropped out of the loop when number reached 6
DO number = 1 TO 10 BY 2
SAY 'Loop' number
SAY 'Hello!'
END
SAY 'Dropped out of the loop when number reached' number
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
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
/******************************* 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
/******************************** 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
DO count = 1 TO 10
IF count = 8
THEN
ITERATE
ELSE
SAY 'Number' count
END
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
PARSE PULL file_name
- The results of the repetitive loops are as follows:
-
1
2
3
Digit is now 4 -
10
8
6
Count is now 4 -
Index is now 10
-
- 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.