Product SiteDocumentation Site

2.27. SELECT


>>-SELECT--+-------------+-;------------------------------------->
           +-LABEL--name-+

   +------------------------------------------------------+
   V                                                      |
>----WHEN--expression--+---+--THEN--+---+--instruction--;-+------>
                       +-;-+        +-;-+

>-+------------------------------------------+-END-+--------+-;-><
  +-OTHERWISE--+---+--+--------------------+-+     +--name--+
               +-;-+  | +----------------+ |
                      | V                | |
                      +---instruction--;-+-+

SELECT conditionally calls one of several alternative instructions.
Each expression after a WHEN is evaluated in turn and must result in 0 or 1. If the result is 1, the instruction following the associated THEN (which can be a complex instruction such as IF, DO, LOOP, or SELECT) is processed and control is then passed to the END. If the result is 0, control is passed to the next WHEN clause.
If none of the WHEN expressions evaluates to 1, control is passed to the instructions, if any, after OTHERWISE. In this situation, the absence of an OTHERWISE produces an error, however, you can omit the instruction list that follows OTHERWISE.

Example 2.41. Instructions - SELECT


balance=100
check=50
balance = balance - check
Select
  when balance > 0 then
    say "Congratulations! You still have" balance "dollars left."
  when balance = 0 then do
    say "Warning, Balance is now zero!  STOP all spending."
    say "You cut it close this month! Hope you do not have any"
    say "checks left outstanding."
    end
  Otherwise do
    say "You have just overdrawn your account."
    say "Your balance now shows" balance "dollars."
    say "Oops!  Hope the bank does not close your account."
    end
end  /* Select */


/

The expression may also be a list of expressions separated by ",". Each subexpression must evaluate to either 0 or 1. The list of expressions is evaluated left-to-right. Evaluation will stop with the first 0 result and 0 will be returned as the condition result. If all of the subexpressions evaluate to 1, then the condition result is also 1.

Example 2.42. Instructions - SELECT

select
  when answer~datatype('w'), answer//2 = 0 Then
     say answer "is even"
  when answer~datatype('w'), answer//2 = 1 Then
     say answer "is odd"
  otherwise
     say answer "is not a number"
end
The example above is not the same as using the following
select
  when answer~datatype('w') & answer//2 = 0 Then
     say answer "is even"
  when answer~datatype('w') & answer//2 = 1 Then
     say answer "is odd"
  otherwise
     say answer "is not a number"
end

The logical & operator will evaluate both terms of the operation, so the term "answer//2" will result in a syntax error if answer is a non-numeric value. With the list conditional form, evaluation will stop with the first false result, so the "answer//2" term will not be evaluated if the datatype test returns 0 (.false).
Notes:
  1. The instruction can be any assignment, command, message instruction, or keyword instruction, including any of the more complex constructs, such as DO, LOOP, IF, or the SELECT instruction itself.
  2. A null clause is not an instruction, so putting an extra semicolon (or label) after a THEN clause is not equivalent to putting a dummy instruction. The NOP instruction is provided for this purpose.
  3. The symbol THEN cannot be used within expression, because the keyword THEN is treated differently in that it need not start a clause. This allows the expression on the WHEN clause to be ended by the THEN without a semicolon (;).