z/OS TSO/E REXX User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


DO FOREVER Loops

z/OS TSO/E REXX User's Guide
SA32-0982-00

Sometimes you might want to purposely write an infinite loop; for instance, in an exec that reads records from a data set until it reaches end of file, or in an exec that interacts with a user until the user enters a particular symbol to end the loop. 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 EXIT Instruction.

Example Using a DO FOREVER Loop

/****************************** REXX *******************************/
/* This exec prints data sets named by a user until the user enters*/
/* a null line.                                                    */
/*******************************************************************/
   DO FOREVER
     SAY 'Enter the name of the next data set or a blank to end.'
     PULL dataset_name
     IF dataset_name = '' THEN
       EXIT
     ELSE
       DO
        "PRINTDS DA("dataset_name")"
         SAY dataset_name 'printed.'
       END
   END

This example sends data sets to the printer and then issues a message that the data set was printed. When the user enters a blank, the loop ends and so does the exec. To end the loop without ending the exec, use the LEAVE instruction, as described in the following topic.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014