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." ENDAssume 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 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." OTHERWISE SAY "It must be > 19." ENDNow 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.
SELECT shortcut
Reginald offers a shortcut that can save typing, and also speed up a SELECT. Consider the following:
SAY "Enter Y or N" PULL My_Variable SELECT WHEN My_Variable == "Y" THEN SAY "Yes" WHEN My_Variable == "N" THEN SAY "No" OTHERWISE SAY "You didn't follow instructions, idiot!" ENDNotice that in every WHEN instruction, a strict equal comparison is being performed. Also notice, that in every WHEN instruction, one side of the equal sign is the same. Specifically, in every WHEN instruction above, the value of My_Variable is checked if it is strictly equal to something else.
In this situation, Reginald allows you to use a shortcut. Since the My_Variable == is used in every WHEN, then you can simply take the part that is the same in every WHEN, omit the ==, and put it after the SELECT keyword. The following is a more compact version of the above:
SAY "Enter Y or N" PULL My_Variable SELECT My_Variable WHEN "Y" THEN SAY "Yes" WHEN "N" THEN SAY "No" OTHERWISE SAY "You didn't follow instructions, idiot!" ENDWhat you put after the SELECT could be any expression, as long as it is the same for every WHEN. For example:
PARSE SOURCE OS . SELECT WHEN LEFT(OS, 3) == "WIN" THEN SAY "Windows" WHEN LEFT(OS, 3) == "UNI" THEN SAY "Unix" WHEN LEFT(OS, 3) == "DOS" THEN SAY "Ms-Dos" OTHERWISE SAY "Other operating system" ENDNote that LEFT(OS, 3) == is used in every WHEN. Therefore, you can compact it as follows:
PARSE SOURCE OS . SELECT LEFT(OS, 3) WHEN "WIN" THEN SAY "Windows" WHEN "UNI" THEN SAY "Unix" WHEN "DOS" THEN SAY "Ms-Dos" OTHERWISE SAY "Other operating system" END