Product Site

2.27. SELECT

SELECT conditionally calls one of several alternative instructions.

Evaluation of the expression list after a WHEN is as follows:
SELECT without CASE

The list of expressions after a WHEN is evaluated left-to-right. Each expression must evaluate to either .false or .true. Evaluation will stop with the first .false result and .false will be returned as the condition result.

If all of the expressions evaluate to .true, then the condition result is also .true.
SELECT CASE

The case_expression is evaluated only once, before the first WHEN instruction is processed. The list of expressions after a WHEN is evaluated left-to-right. Each expression is compared to the result of case_expression using "==". Evaluation will stop with the first .true result and .true will be returned as the condition result.

If all comparisons evaluate to .false, then the condition result is also .false.
If the result from above is .true, 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 .false, control is passed to the next WHEN clause.

If none of the WHEN results are .true, 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.35. 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 */

Example 2.36. 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 .false.
Example 2.37. Instructions — SELECT CASE
select case random(6)
  when 1 then say "bad luck!"
  when 5, 6 then say "great!"
  otherwise say "try again"
end

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. Except when within a bracketed subexpression, 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 (;).