The SELECT keyword is used when you want to select one of several possible Conditional instructions to execute (and skip all other conditional instructions within the group). The keywords WHEN and THEN are used for each Conditional instruction much the same way that IF/THEN is used to form a Conditional. In other words, the WHEN goes before the expression to be tested, and the THEN goes before the instruction to be executed if the expression is true. The SELECT keyword gets placed before all of the Conditional instructions in the group. An END keyword is placed after the last Conditional instruction (ie, it marks the end of the SELECT group).

SELECT is very useful if you wish to check some variable for a large number of expressions, and execute instructions for only one of those expressions. Consider the following:

/* Check My_Variable for a variety of expressions,
   but execute instructions for only 1 expression. */
SAY "Enter a number"
PULL My_Variable
SELECT
   WHEN My_Variable = 10 THEN SAY "It's equal to 10."
   WHEN My_Variable < 10 THEN SAY "It's less than 10."
   WHEN My_Variable < 20 THEN SAY "It's less than 20."
END
Assume that My_Variable's value is 5. REXX checks the first WHEN's expression (ie, Is My_Variable equal to 10?). This is false, so the remainder of that WHEN Conditional instruction is skipped (ie, It's equal to 10 is not printed to the screen). Because the first Conditional was false, REXX proceeds to the next Conditional. It checks if the second WHEN's expression is true (ie, Is My_Variable < 10?). This is true. Therefore, REXX executes the instruction after that WHEN's respective THEN (ie, It's less than 10 is printed to the screen). REXX then immediately jumps to END. It skips the remaining conditional instructions (ie, the third WHEN's expression is never even tested, and therefore It's less than 20 is not printed to the screen).

Assume that My_Variable's value is 10. REXX checks the first WHEN conditional expression (ie, Is My_Variable equal to 10?). This is true. Therefore, REXX executes the instruction after that WHEN's respective THEN (ie, It's equal to 10 is printed to the screen). REXX then immediately jumps to END. It skips the remaining conditional instructions (ie, the second and third WHEN's expressions are never even tested).

Assume that My_Variable's value is 25. REXX checks the first WHEN conditional expression (ie, Is My_Variable equal to 10?). This is false, so the remainder of that WHEN conditional is skipped (ie, It's equal to 10 is not printed to the screen). Because the first conditional was false, REXX proceeds to the next conditional. It checks if the second WHEN's expression is true (ie, Is My_Variable < 10?). This is also false. Therefore, REXX skips the respective THEN for that WHEN. Because the second conditional was false, REXX proceeds to the next conditional. It checks if the third WHEN's expression is true (ie, Is My_Variable < 20?). This is also false. There are no more conditional instructions. At this point, REXX will raise a SYNTAX condition. You must always make sure that every time a SELECT group is executed, one of its conditionals turns out to be true, otherwise REXX will raise a SYNTAX condition. If you don't want REXX to raise a SYNTAX condition for any instance where all of the conditionals turn out to be false, you can use the OTHERWISE keyword after the last conditional (ie, before the END keyword), and optionally follow it with some REXX instruction. This OTHERWISE and its associated instruction are executed only when all of the conditionals in the SELECT turn out to be false.

/* Check My_Variable for a variety of conditions,
  but execute instructions for only 1 condition. */
SAY "Enter a number"
PULL My_Variable
SELECT
   WHEN My_Variable = 10 THEN SAY "It's equal to 10."
   WHEN My_Variable < 10 THEN SAY "It's less than 10."
   WHEN My_Variable < 20 THEN SAY "It's less than 20."
   OTHERWISE SAY "It must be > 19."
END
Now when My_Variable is 25, the above OTHERWISE gets executed (ie, the SAY instruction printing It must be > 19 is executed). By contrast, when using IF/THEN with ELSE keywords, it's OK if the IF/THEN as well as all of its subsequent ELSE instructions turn out to be false.