Exercise - Using a DO WHILE Loop
Write an exec with a DO WHILE loop that asks passengers on a commuter airline if
they want a window seat and keeps track of their responses. The flight has 8
passengers and 4 window seats. Discontinue the loop when all the window seats
are taken. After the loop ends, display the number of window seats taken and the
number of passengers questioned.
ANSWER
Possible Solution
/******************************** REXX *****************************/
/* This exec uses a DO WHILE 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 WHILE (passenger < 8) & (window_seats \= 4)
/****************************************************************/
/* Continue while you have not questioned all 8 passengers and */
/* while all the window seats are not taken. */
/****************************************************************/
SAY 'Do you want a window seat? Please answer Y or N.'
PULL answer
passenger = passenger + 1
/* Increase the number of passengers by 1 */
IF answer = 'Y' THEN
window_seats = window_seats + 1
/* Increase the number of window seats by 1 */
ELSE NOP
END
SAY window_seats 'window seats were assigned.'
SAY passenger 'passengers were questioned.'
DO UNTIL Loops
DO UNTIL loops in a flowchart appear as follows:
As REXX instructions, the flowchart example looks like:
DO UNTIL
instruction(s)
expression
False
True
END
Using Looping Instructions
Chapter 4. Controlling the Flow Within an Exec 53