case

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.

There are three types of criteria you can use for comparison:

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.

Syntax

Syntax diagram for the case statement
label
A label, followed by a colon, which an exit statement can reference. For more information, see Conditional and loop statements.
criterion
A variable, constant, expression, literal, or system variable (such as converseVar.eventKey or sysVar.systemType).

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.

when
The beginning of a clause that is invoked only in one of the following cases:
  • You specified a criterion, and the when clause is the first to contain a matchExpression that is equal to the criterion.
  • You did not specify a criterion, and the when clause is the first to contain a logical expression that evaluates to TRUE.

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.

matchExpression
An expression to which the criterion is compared. For more information, see "Match expressions" in this topic.
logicalExpression
An expression that resolves to either TRUE or FALSE. For more information, see "Logical expressions."
statement
A statement to be run if EGL matches the associated when expression or reaches the associated otherwise clause.
otherwise
The beginning of a clause that EGL invokes if unable to match a when expression.

Example

The following example of a case statement includes multiple match expressions in the second when clause (2, 3, 4):
  case (myRecord.requestID)
    when (1)
      myFirstFunction();
    when (2, 3, 4)
      try
        call myProgram;
      onException(iex InvocationException)
        myCallFunction(fileEx);
      end
    otherwise
      myDefaultFunction();
  end  
The following example shows a case statement with no criterion:
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.


Feedback