Using Conditional Instructions

There are two types of conditional instructions:

IF...THEN...ELSE Instructions

The examples of IF...THEN...ELSE instructions in previous chapters demonstrate the two-choice selection. In a flow chart, this appears as follows:

dfhrx001

As a REXX instruction, the flowchart example looks like:

IF expression THEN instruction
              ELSE instruction

You can also arrange the clauses in one of the following ways to enhance readability:

    IF expression THEN
       instruction
    ELSE
       instruction

or

    IF expression
      THEN
        instruction
      ELSE
        instruction

When you put the entire instruction on one line, you must use a semicolon before the ELSE to separate the THEN clause from the ELSE clause.

IF expression THEN instruction; ELSE instruction

Generally, at least one instruction should follow the THEN and ELSE clauses. When either clause has no instructions, it is good programming practice to include NOP (no operation) next to the clause.

IF expression THEN
   instruction
ELSE NOP

If you have more than one instruction for a condition, begin the set of instructions with a DO and end them with an END.

IF weather = rainy THEN
   SAY 'Find a good book.'
ELSE
   DO
     PULL playgolf     /* Gets data from input stream */
     If playgolf='YES' THEN SAY 'Fore!'
   END

Without the enclosing DO and END, the language processor assumes only one instruction for the ELSE clause.

Nested IF...THEN...ELSE Instructions

Sometimes it is necessary to have one or more IF...THEN...ELSE instructions within other IF...THEN...ELSE instructions. Having one type of instruction within another is called nesting. With nested IF instructions, it is important to match each IF with an ELSE and each DO with an END.

IF weather = fine THEN
   DO
      SAY 'What a lovely day!'
      IF tenniscourt = free THEN
         SAY 'Let''s play tennis!'
      ELSE NOP
   END
ELSE
   SAY 'We should take our raincoats!'

Not matching nested IFs to ELSEs and DOs to ENDs can have some surprising results. If you eliminate the DOs and ENDs and the ELSE NOP, as in the following example, what is the outcome?

Figure 17. Example of Missing Instructions
/******************************** REXX *******************************/
/* This program demonstrates what can happen when you do not include */
/* DOs, ENDs, and ELSEs in nested IF...THEN...ELSE instructions.     */
/*********************************************************************/
   weather = 'fine'
   tenniscourt = 'occupied'

   IF weather = 'fine' THEN
      SAY 'What a lovely day!'
      IF tenniscourt = 'free' THEN
         SAY 'Let''s play tennis!'
   ELSE
      SAY 'We should take our raincoats!'

Looking at the program you might assume the ELSE belongs to the first IF. However, the language processor associates an ELSE with the nearest unpaired IF. The outcome is as follows:

What a lovely day!
We should take our raincoats!

Exercise - Using the IF...THEN...ELSE Instruction

Write the REXX instructions for the following flowchart:

dfhrx002

ANSWER

IF a = 0 THEN
   IF c = 2 THEN
      z = 1
   ELSE NOP
ELSE
   IF z = 2 THEN
     IF c = 3 THEN
        a = 1
     ELSE
        a = 3
   ELSE NOP

SELECT WHEN...OTHERWISE...END Instruction

To select one of any number of choices, use the SELECT WHEN...OTHERWISE...END instruction. In a flowchart it appears as follows:

dfhrx003

As a REXX instruction, the flowchart example looks like:

SELECT
   WHEN  expression  THEN  instruction
   WHEN  expression  THEN  instruction
   WHEN  expression  THEN  instruction
   :
   :
   OTHERWISE
     instruction(s)
END

The language processor scans the WHEN clauses starting at the beginning until it finds a true expression. After it finds a true expression, it ignores all other possibilities, even though they might also be true. If no WHEN expressions are true, it processes the instructions following the OTHERWISE clause.

As with IF...THEN...ELSE, when you have more than one instruction for a possible path, begin the set of instructions with a DO and end them with an END. However, if more than one instruction follows the OTHERWISE keyword, DO and END are not necessary.

Figure 18. Example Using SELECT WHEN...OTHERWISE...END
/******************************** REXX *******************************/
/* This program receives input with a person's age and sex.  In      */
/* reply, it produces a person's status as follows:                  */
/*      BABIES    -  under 5                                         */
/*      GIRLS     -  female 5 to 12                                  */
/*      BOYS      -  male 5 to 12                                    */
/*      TEENAGERS -  13 through 19                                   */
/*      WOMEN     -  female 20 and up                                */
/*      MEN       -  male 20 and up                                  */
/*********************************************************************/
 PARSE ARG age sex .

 SELECT
   WHEN age < 5 THEN                /* person younger than 5 */
     status = 'BABY'
   WHEN age < 13 THEN               /* person between 5 and 12 */
     DO
       IF sex = 'M' THEN            /* boy between 5 and 12  */
          status = 'BOY'
       ELSE                         /* girl between 5 and 12 */
          status = 'GIRL'
     END
   WHEN age < 20 THEN               /* person between 13 and 19 */
     status = 'TEENAGER'
   OTHERWISE
     IF sex = 'M' THEN              /* man 20 or older */
       status = 'MAN'
     ELSE                           /* woman 20 or older */
       status = 'WOMAN'
 END

 SAY 'This person should be counted as a' status'.'

Each SELECT must end with an END. Indenting each WHEN makes a program easier to read.

Exercises - Using SELECT WHEN...OTHERWISE...END

"Thirty days hath September, April, June, and November; all the rest have thirty-one, save February alone ..."

Write a program that uses the input of a number from 1 to 12, representing the month, and produces the number of days in that month. Assume the user specifies the month number as an argument when calling the program. (Include in the program an ARG instruction to assign the month number into the variable month). Then have the program produce the number of days. For month 2, this can be 28 or 29.

ANSWER

Figure 19. Possible Solution
/******************************** REXX *******************************/
/* This program uses the input of a whole number from 1 to 12 that   */
/* represents a month.  It produces the number of days in that       */
/* month.                                                            */
/*********************************************************************/

 ARG month

 SELECT
   WHEN month = 9 THEN
     days = 30
   WHEN month = 4 THEN
     days = 30
   WHEN month = 6 THEN
     days = 30
   WHEN month = 11 THEN
     days = 30
   WHEN month = 2 THEN
     days = '28 or 29'
   OTHERWISE
     days = 31
 END

 SAY 'There are' days 'days in Month' month'.'