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 1. 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.


Reference Reference

Feedback


Timestamp icon Last updated: Tuesday, 7 January 2014


http://pic.dhe.ibm.com/infocenter/cicsts/v5r1/topic/com.ibm.cics.rexx.doc//dfhrx/rvse021.html