The CASE statement allows applying a range of conditions to data selection. It is possible to include more than two conditions into the CASE statement which makes it more powerful than the IF statement. Case statement is similar to a set of IF loops.
Expression |
It is an optional 4GL expression |
Declaration clause |
An optional clause where values and functions local to the CASE statement can be declared |
WHEN clause |
One or more WHEN clauses which contain a 4GL expression and a number of executable statements |
There are two types of CASE statements depending on whether the optional 4GL expression follows the CASE keyword:
CASE statement is followed by a 4GL expression |
You must specify an INT, SMALLINT, DECIMAL, CHAR(1) or VARCHAR(1) expression in the WHEN block. 4GL executes the WHEN clause, if the expression following CASE statement and the expression specified in this WHEN clause return the same value and if that value is not NULL. |
CASE statement is not followed by an expression |
Boolean expressions must be specified in the WHEN clauses. If it is evaluated as TRUE, the WHEN statement is executed. |
Here is an example of the CASE statement type I:
CASE result
WHEN 1
CALL open()
WHEN 2
EXIT CASE
OTHERWISE
CALL retry()
END CASE
The next example is an example of the CASE statement type II:
CASE
WHEN answer MATCHES "[Yy]"
CALL open()
WHEN answer MATCHES "[Nn]"
EXIT CASE
OTHERWISE
CALL retry()
END CASE
The END CASE statement must indicate the end of the CASE statement, it must be placed after all WHEN blocks and after OTHERWISE block, if it is present.