The CASE construct performs a conditional selection of a sequence of statements.
Format
BEGIN CASE CASE expr statements CASE expr statements . . . END CASE |
Parameter(s)
expr |
An expression to be evaluated for its logical value. |
statement |
Statements to be executed if the previous expr had been tested to be logically true. |
Description
A CASE construct must begin with a BEGIN CASE statement and end with an END CASE statement. The CASE construct evaluates a series of conditions until one is true and executes a set of statements accordingly. The expressions in the CASE statements are evaluated sequentially for their logical value until a value of true is encountered. When an expression evaluates to true, the statements between the CASE statement and the next CASE statement are executed, and all subsequent CASE statements are skipped. Execution continues with the next sequential statement following the END CASE statement.
If none of the expressions evaluate to true, no action is performed, and program execution continues with the statement after the END CASE statement.
The CASE statement can usually replace multiple nested IF constructs: it is much more readable and easier to implement.
Example
To test a variable NUMBER for positive or negative value, the source code might read:
BEGIN CASE CASE NUMBER > 0 PRINT "POSITIVE" CASE NUMBER < 0 PRINT "NEGATIVE" CASE 1 PRINT "ZERO" END CASE |
Note that the third and last condition reads CASE 1 instead of CASE NUMBER = 0. In this situation the two conditions are equivalent since the last condition would only be tested if the first two failed. CASE 1 is often used as the last condition of a CASE statement, as a catch-all condition.
See Also