Skip to main content

Software > Software Development > IBM REXX Family >

NetRexx

Technical detail

Select instruction

select [label name] [protect term]; whenlist
[otherwise[;] instructionlist]
[catch [vare =] exception;
instructionlist]...
[finally[;]
instructionlist]
end [name];
   where name is a non-numeric symbol
and whenlist is one or more whenconstructs
and whenconstructis:
     when expression[;] then[;] instruction

   and instructionlist is zero or more instructions.

select is used to conditionally execute one of several alternatives. The construct may optionally be given a label, and may protect an object while the instructions in the construct are executed; exceptional conditions can be handled with catch and finally, which follow the body of the construct.

Each expression following a when is evaluated in turn and must result in either 0 or 1. If the result is 1, then the instruction following the associated then (which may be a complex instruction such as if, do, loop, or select) is executed and control will then pass directly to the end. If the result is 0, control will pass to the next when clause.

If none of the when expressions result in 1, then control will pass to the instruction list (if any) following otherwise. In this situation, the absence of an otherwise is a run-time error.[1] 

Notes:

  1. An instruction may be any assignment, method call, or keyword instruction, including any of the more complex constructions such as do, loop, if, and the select instruction itself. A null clause is not an instruction, however, so putting an extra semicolon after the then is not equivalent to putting a dummy instruction (as it would be in C or PL/I). The nop instruction is provided for this purpose.
  2. The keyword then is treated specially, in that it need not start a clause. This allows the expression on the when clause to be terminated by the then, without a ';' being required -- were this not so, people used to other computer languages would be inconvenienced. Hence the symbol then cannot be used as a variable name within the expression.[2] 

Label phrase

If label is used to specify a name for the select group, then a leave instruction which specifies that name may be used to leave the group, and the end that ends the group may optionally specify the name of the group for additional checking.

Example:

  select label roman
when a=b then say 'same'
when a<b then say 'lo'
otherwise
say 'hi'
if a=0 then leave roman
say 'a non-0'
end roman

In this example, if the variable a has the value 0 and b is negative then just 'hi' is displayed.

Protect phrase

If protect is given it must be followed by a term that evaluates to a value that is not just a type and is not of a primitive type; while the select construct is being executed, the value (object) is protected -- that is, all the instructions in the select construct have exclusive access to the object.

Both label and protect may be specified, in any order, if required.

Exceptions in select constructs

Exceptions that are raised by the instructions within the body of the group may be caught using one or more catch clauses that name the exception that they will catch. When an exception is caught, the exception object that holds the details of the exception may optionally be assigned to a variable, vare.

Similarly, a finally clause may be used to introduce instructions that will always be executed at the end of the select group, even if an exception is raised (whether caught or not).

The Exceptions section has details and examples of catch and finally.

Footnotes:

[1]  In the reference implementation, a NoOtherwiseException is raised.

[2]  Strictly speaking, then should only be recognized if not the name of a variable. In this special case, however, NetRexx language processors are permitted to treat then as reserved in the context of a when clause, to provide better performance and more useful error reporting.

 

 

PreviousTable of contents Next
We're here to help
Easy ways to get the answers you need.
E-mail us

or call us at
877-426-3774
Priority code:
104CBW67