The select instruction group performs an instruction (or sequence of instructions) among a set of possible alternatives. The select instruction group has the following syntactic structure.
select
when booleanExpression then instruction(s) when booleanExpression then instruction(s) ...etc. [ otherwise [ instructions ] ] end |
Each when clause identifies a boolean expression to evaluate. A boolean expression is true if the value is '1', and false if the value is '0'. Any other values are invalid.
The booleanExpression is concluded when the end of the source line containing the when keyword is encountered. The source line may have been continued across multiple lines. If the then keyword is encountered on the same source line, or continued source lines, the booleanExpression is immediately concluded. Thus, the then keyword is special and cannot be used within the context of the booleanExpression.
At least one instruction must follow each then keyword. If multiple instructions follow the then keyword these must be bracketed within a do group.
The instructions that follow the otherwise keyword are optional. When multiple instructions are present these do NOT require an enclosing do group.
There must be at least one when phase. An otherwise clause is optional. HOWEVER if an otherwise clause is absent, AND if none of the when clauses evaluate to be true, then error code 7 (when or otherwise expected) is raised. An otherwise clause required in this circumstance. Hint: always provide and otherwise clause -- just in case.
After encountering the first when clause which evaluates to be true, the instruction(s) following the then keyword is/are performed. When the instruction(s) complete, processing continues with the instruction following the end instruction that is associated with the selectinstruction group. If none of the when clauses evaluate to be true, the optional instruction(s) following the otherwise keyword is/are performed. After these complete, processing continues with the instruction following the end instruction.
The following shows how a select instruction group can be used to validate program arguments.
/* validate program arguments */ arg number option +1 . select when number = '' then call usagemsg 'Please specify a whole number' when datatype( number, 'Wholenumber' ) = 0 then call usagemsg 'Please specify a whole number' when option = '' then nop when verify( option, 'ABC', 'Match' ) > 0 then nop otherwise call usagemsg "Supported options are 'A', 'B', or 'C'" end /* procede with program processing ... */ return 0 usagemsg : procedure say 'Note:' arg(1) say say 'Usage:' say " rexx program wholeNumber [option, one of 'A', 'B', or 'C']" return |
Other programming languages use isolated semicolons as empty instructions. In Rexx programs, isolated semicolons are not empty instructions. The NOP instruction is explicitly used for this purpose instead. Following the then keyword of a when clause an instruction must be provided. When an instruction is absent error code 9 (unexpected when or otherwise) is raised. For example, the following program is erroneous:
say 'Please enter your favorite color:'
parse pull color select when color = 'RED' then ; /* <-- ERROR !!! */ otherwise say 'your favorite color is not red' end |
The correct way to program this in Rexx uses the NOP instruction instead.
say 'Please enter your favorite color:'
parse pull color select when color = 'RED' then nop /* <-- ah, this is correct */ otherwise say 'your favorite color is not red' end |
A name is not permitted with the end instruction, when it concludes a select instruction group.