CASE (selective execution structure)

Top  Previous  Next

 

CASE condition

OF expression [ TO expression ]

statements

[ OROF expression [ TO expression ] ]

statements

[ ELSE ]

statements

END

CASE

Initiates a selective execution structure.

condition

A numeric or string variable or expression.

OF

The statements following an OF are executed when the expression following the OF option is equal to the condition of the CASE. There may be many OF options in a CASE structure.

expression

A numeric or string constant, variable, or expression.

TO

TO allows a range of values in an OF or OROF. The statements following the OF (or OROF) are executed if the value of the condition falls within the inclusive range specified by the expressions. The expression following OF (or OROF) must contain the lower limit of the range. The expression following TO must contain the upper limit of the range.

OROF

The statements following an OROF are executed when either the expression following the OROF or the OF option is equal to the condition of the CASE. There may be many OROF options associated with one OF option. An OROF may optionally be put on a separate line. An OROF does not terminate preceding statements groups, so control "falls into" the OROF statements.

ELSE

The statements following ELSE are executed when all preceding OF and OROF options have been evaluated as not equivalent. ELSE is not required; however, when used, it must be the last option in the CASE structure.

statements

Any valid Clarion executable source code.

A CASE structure selectively executes the first set of statements encountered for which there is equivalence between the condition and expression or range of expressions. CASE structures may be nested within other executable structures and other executable structures may be nested within CASE structures. The CASE structure must terminate with an END statement (or period).

For those situations where the program's logic could allow using either a CASE structure or a complex IF/ELSIF structure, the CASE structure will generally generate more efficient object code. EXECUTE generates the most efficient object code for those special cases where the condition evaluates to an integer in the range of 1 to n.

Regarding the use of the OF-TO and OROF-TO clauses, both expressions are evaluated even if the value of the CASE-expression is less than the lower boundary of the specified range.

 

For example:

 

 CASE A

 OF Func1() TO Func2()

  ...

 END

 

In this case, both Func1() and Func2() are called even if A < Func1().

 

Example:

CASE ACCEPTED()                 !Evaluate field edit routine

OF ?Name                        !If field is Name

ERASE(?Address,?Zip)           !erase Address through Zip

GET(NameFile,NameKey)          !get the record

 

CASE Action                    !Evaluate Action

OF 1                           !adding record - does not exist

 IF NOT ERRORCODE()            !should be a file error

  ErrMsg = 'ALREADY ON FILE'   !otherwise display error message

  DISPLAY(?Address,?Zip)       !display address through zipcode

  SELECT(?Name)                !re-enter the name

 END      

OF 2 OROF 3                    !change or delete - record exists

 DISPLAY(?Address,?Zip)        !display address through zipcode

END                            !end case action

 

CASE Name[1]                   !Get first letter of name

OF 'A' TO 'M'                  !Process first half of alphabet

OROF 'a' TO 'm'

 DO FirstHalf

OF 'N' TO 'Z' OROF 'n' TO 'z'  !Process second half of alphabet

 DO SecondHalf

END                            !End case sub(name

 

OF ?Address                     !If field is address

DO AddressVal                  !call validation routine

END                             !End case accepted()

 

See Also:

EXECUTE

IF