CASE construct

The CASE construct provides an alternative to an IF construct containing multiple ELSEIFs. It takes the general form:

 SELECT CASE(expression)
  CASE(case_selector 1)
           statement_block 1
  CASE(case_selector 2)
            statement_block 2
  . . .
  CASE DEFAULT
            statement_block
 END SELECT

CASE constructs can be named.

The expression provides a value upon which the selection is based and must be a scalar of type INTEGER, LOGICAL or CHARACTER.

CASE DEFAULT is optional and is not necessarily placed last. It provides for the situation where none of the given CASEs applies.

The case_selector values must be of the same type as expression. They contain a single constant value or a list of constant values. Ranges may also be used. For example:

!Month and Days are of type INTEGER
!Month has been assigned a value.
SELECT CASE(Month)
  CASE(4,6,9,11)
    Days=30
  CASE(1,3,5,7:8,10,12)
    Days=31
  CASE(2)
    Days=28
    !Code to test for a leap year
  CASE DEFAULT
    !Code to flag an error
END SELECT

A range takes the form CASE(low: high) where low or high (but not both) may be omitted. For example, if high is omitted then the condition becomes expression>low. For example:

SELECT CASE(n)
  CASE(:-1)
    Sign=-1 !if n<0
  CASE(0)
    Sign=0 !if n=0
  CASE(1:)
    Sign=1 !if n>0
END SELECT

Each case must be unique with no possible overlap.

In the case of a CHARACTER expression, it may represent a single character or a character string.

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited