Combining types of loops, Nested do loops, Combining types of loops nested do loops – IBM SC34-5764-01 User Manual

Page 68

Advertising
background image

ANSWER

Combining Types of Loops

You can combine repetitive and conditional loops to create a compound loop. The following loop is set to
repeat 10 times while the quantity is less than 50, at which point it stops.

quantity = 20
DO number = 1 TO 10 WHILE quantity < 50

quantity = quantity + number
SAY 'Quantity = 'quantity '

(Loop 'number')'

END

The result of this example is as follows:

Quantity = 21

(Loop 1)

Quantity = 23

(Loop 2)

Quantity = 26

(Loop 3)

Quantity = 30

(Loop 4)

Quantity = 35

(Loop 5)

Quantity = 41

(Loop 6)

Quantity = 48

(Loop 7)

Quantity = 56

(Loop 8)

You can substitute a DO UNTIL loop, change the comparison operator from < to >, and get the same
results.

quantity = 20
DO number = 1 TO 10 UNTIL quantity > 50

quantity = quantity + number
SAY 'Quantity = 'quantity '

(Loop 'number')'

END

Nested DO Loops

Like nested IF...THEN...ELSE instructions, DO loops can contain other DO loops. A simple example
follows:

/******************************** REXX *******************************/
/* This program uses a DO UNTIL loop to keep track of window seats

*/

/* in an 8-seat commuter airline.

*/

/*********************************************************************/

window_seats = 0

/* Initialize window seats to 0 */

passenger = 0

/* Initialize passengers to 0

*/

DO UNTIL (passenger >= 8) | (window_seats = 4)

/******************************************************************/
/* Continue while the program has not yet read the responses of

*/

/* all 8 passengers and while all the window seats are not taken. */
/******************************************************************/

PULL window

/* Gets "Y" or "N" from input stream

*/

passenger = passenger + 1

/* Increase number of passengers by 1 */

IF window = 'Y' THEN

window_seats = window_seats + 1 /* Increase window seats by 1 */

ELSE NOP

END
SAY window_seats 'window seats were assigned.'
SAY passenger 'passengers were questioned.'

Figure 25. Possible Solution

Control Flow within a Program

46

CICS TS for VSE/ESA: REXX Guide

Advertising