DO WHILE Loops
DO WHILE loops in a flowchart appear as follows:
DO WHILE
Tr ue
instruction(s)
expression
False
END
As REXX instructions, the flowchart example looks like:
DO WHILE expression /* expression must be true */
instruction(s)
END
Use a DO WHILE loop when you want to execute the loop while a condition is true. DO WHILE tests the
condition at the top of the loop. If the condition is initially false, the language processor never executes the
loop.
You can use a DO WHILE loop instead of the DO FOREVER loop in the example of using the LEAVE
instruction on page 41. However, you need to initialize the loop with a first case so the condition can be
tested before you get into the loop. Notice the first case initialization in the first PULL of the following
example.
Exercise - Using a DO WHILE Loop
Write a program with a DO WHILE loop that uses as input a list of responses about whether passengers
on a commuter airline want a window seat. The flight has 8 passengers and 4 window seats. Discontinue
the loop when all the window seats are taken. After the loop ends, produce the number of window seats
taken and the number of responses processed.
ANSWER
/******************************** REXX *******************************/
/* This program uses a DO WHILE loop to send a string to a */
/* user-written function for processing. */
/*********************************************************************/
PULL string /* Gets string from input stream */
DO WHILE string \= 'QUIT'
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
PULL string
END
SAY 'Program run complete.'
Figure 22. Example Using DO WHILE
Control Flow within a Program
Chapter 4. Controlling the Flow within a program 43