CASE

The CASE statement specifies statement blocks to be executed conditionally, depending on the value of an expression. Unlike IF statements, CASE does not restrict the logical flow of control to only two branches.

4GLREF00000113.gif

statement is an SQL statement or other 4GL statement.

The CASE statement is equivalent to a set of nested IF statements.

You can specify two types of CASE statements:

If you specify an expression following the CASE keyword, you must also specify INT, SMALLINT, DECIMAL, CHAR(1), or VARCHAR(1) expressions in the WHEN block. (The syntax diagram indicates a subset of general 4GL expressions because of these data type restrictions.) 4GL executes the statement block if both expressions return the same non-NULL value.

If no expression follows the CASE keyword, the WHEN block must specify a 4GL Boolean expression; if this returns TRUE, then the WHEN block is executed. This form of CASE typically executes more quickly than the equivalent CASE expression form.

The WHEN Blocks

Each WHEN block specifies an expression and a block of one or more associated statements. The WHEN block has the following syntax:

4GLREF00000114.gif

statement is an SQL statement or other 4GL statement.

The data type of the expression that you use in the WHEN blocks depends on whether or not you include an expression following the CASE keyword.

If you omit the CASE expression, then you must include a Boolean expression (one that returns either TRUE or FALSE or NULL) in each of the WHEN blocks. If the Boolean expression is TRUE (non-zero), 4GL executes the corresponding block of statements.

The OTHERWISE Block

The OTHERWISE keyword specifies statements to be executed when 4GL does not find a matching WHEN block to execute. It has this syntax:

4GLREF00000115.gif

statement is an SQL statement or other 4GL statement.

4GL executes the OTHERWISE block only if it cannot execute any of the WHEN blocks. If you include the OTHERWISE block, it must follow the last WHEN block.

References

FOR

IF

WHILE