The EGL case statement provides an alternative to multiple nested if statements. Based on one or more evaluations, EGL selects one set of statements to run from various alternatives. The case statement is similar to a C switch or a COBOL EVALUATE statement.
The statement has two main forms. In the first version, EGL evaluates a single criterion and looks for a matching value among the when clauses. In the second version, you do not supply a top-level criterion and EGL evaluates a logical expression for each when statement in turn, running the code for the first expression that evaluates as true. In the first version, the only condition EGL evaluates is simple equality to the criterion. The second condition offers greater flexibility and comparisons based on inequalities, ranges of values, and so on.
No other criteria are valid. For example, you cannot compare one record to another, or compare an I/O object (like a SerialRecord) to endOfFile.
As in COBOL, but not as in C, control does not fall through from one when clause to the next. After the statements run in a when or otherwise clause, control passes to the EGL statement that immediately follows the end of the case statement. If EGL does not match a when clause or find an otherwise clause, control also passes to the next statement following the end of the case statement, without causing an error.
If a single clause includes multiple match expressions (see "Example" in this topic), EGL evaluates those expressions from left to right, and stops when it finds a match.
If you specify criterion (first syntax diagram), each of the subsequent when clauses must contain one or more instances of matchExpression. If you do not specify a criterion (second syntax diagram), each of the subsequent when clauses must contain a logical expression.
If you code the clause without EGL statements, control passes from the case loop without invoking the otherwise clause.
A case statement may have any number of when clauses.
case (myRecord.requestID) when (1) myFirstFunction(); when (2, 3, 4) try call myProgram; onException(iex InvocationException) myCallFunction(fileEx); end otherwise myDefaultFunction(); end
Program calc3 x INT = 3; y INT = 5; z INT = 7; function main() case when (x == 3) writeStdOut("x passes"); when (y == 5) writeStdOut("y passes"); when (z == 7) writeStdOut("z passes"); otherwise writeStdErr("You should not see this msg"); end end
The console shows the words "x passes". The case statement ends when a TRUE expression is found.