CASE-ENDCASE

Select a statement from a set of statements

WTSupported in traditional Synergy on Windows
WNSupported in Synergy .NET on Windows
USupported on UNIX
VSupported on OpenVMS
CASE expression OF
  BEGINCASE
  [label_1:]  statement_1
  .
  .
  .
  [label_n:]  statement_n
  ENDCASE
[ELSE exception]

Arguments

expression

An expression whose value determines which statement to execute. (a or n)

label_1

(optional) A label that identifies the first statement in the set of statements from which to select.

statement_1

The first statement in the set that can be selected according to the result of expression.

label_n

(optional) A label that identifies the nth statement in the set of statements from which to select.

statement_n

The nth statement in the set that can be selected according to the result of expression.

exception

(optional) A statement to be executed if the value of variable does not select a statement in the set. If a statement is selected from the set, the optional ELSE is ignored.

Discussion

CASE selects a statement from a set of statements for execution. It consists of a selection variable, a set of statements from which one is to be executed, and an optional ELSE statement (which is executed if no statement is selected from the set).

Three types of CASE statements exist:

In an unlabeled CASE statement, no statement has a label (label_1 through label_n are omitted). Expression is non-implied numeric and is interpreted as an ordinal number 1–n, where n is the number of statements in the set. For example, a value of 6 selects the sixth statement in the set.

In a labeled CASE statement, all statements are identified with labels. The labels must all be literals and must be the same type as expression (alpha or numeric). Implied labels are allowed with implied numeric expressions only. The label (label_1, label_2, and so forth) can be an alpha literal, a numeric literal, or two numeric literals separated by a hyphen (-). In the latter case, the value of the first literal must be less than or equal to the value of the second literal; a selection value that falls within this range selects the associated statement.

When using a CASE selection variable that is not implied (i, d, p, or n), only integer or decimal values are allowed for labels. If the selection variable is declared as n and an implied variable is passed to the routine and used in a CASE statement, the implied variable will be rounded (following normal rounding rules) before being compared to the CASE labels.

When using a CASE selection variable that is implied (d., p., or n.), labels can be either integer, decimal, or implied values. Implied values will only match a label with the same exact value or a label range that includes the value.

Note

If you use alpha labels with string control variables (and don’t compile with -qnoargnopt), be aware that the comparisons are .EQ. and .NE., which will be compared for the length of the shortest field. If you would like to match against the exact label, you should use the USING statement with the label (.EQS.“ABC”). Synergy .NET acts as if -qnoargnopt is set.

Note

CASE labels cannot be referenced by program control statements like CALL and GOTO.

After a statement is selected and executed, control is transferred to the statement following the CASE. If a selected statement is a compound statement, execution proceeds to the end of the compound statement, and then control is transferred to the statement following the CASE.

If the result of expression exceeds the decimal range 1-n (where n is the number of statements in the set) or doesn’t match a case label, control is transferred to the ELSE. If no ELSE exists, no statement is executed and the entire CASE statement is skipped.

Tip

Although CASE is functionally equivalent to a cascaded IF-THEN-ELSE statement, when using a numeric expression, CASE is more efficient and easier to maintain. The USING statement is as fast as an unlabeled CASE statement but provides more maintainable and readable code. CASE is most efficient when using an i or d expression for selection.

See also

Examples

The following unlabeled CASE statement consists of four selectable statements.

function math
    a_select    ,n
    a_v1        ,n
    a_v2        ,n
record
    result      ,i2
proc
    case a_select of
      begincase
        result = a_v1 + a_v2                    ;Executed if a_select = 1
        result = a_v1 - a_v2                    ;Executed if a_select = 2
        result = a_v1 * a_v2                    ;Executed if a_select = 3
        result = a_v1 / a_v2                    ;Executed if a_select = 4
      endcase
    else
      clear result
    freturn result
endfunction

If a_select has a value other than 1, 2, 3, or 4, the statement following the ELSE is executed.

The next example consists of three selectable statements. The first is a simple assignment statement that is executed if a_select has a value 2. The second is a compound statement that is selected if a_select has a value of 5, 6, or 7. The third is also a compound statement that is selected by an a_select value of -1. If a_select has a value other than 2, 5, 6, 7, or -1, no statement is executed.

    case a_select of
      begincase
    2:
        v3 = v1 - v2
    5-7:
        begin
          v3 = v1 + v2
          v4 = v3 * factr4
        end
    -1:
        begin
          write(TTCHN, varrec, recloc)
          call varinit
          varopt = 17
        end
      endcase

In the following example, the CASE statement interprets option keywords entered by the user. In this example, if the user enters an option that is not in the set of options, control transfers to the ELSE statement.

function math
    a_v1        ,n
    a_v2        ,n
record
    option      ,a4
    result      ,i2

proc
    repeat
      begin
        display(TTCHN, "Enter option:    ")
        reads(TTCHN, option) [eof=done]
        case option of
          begincase
          "HELP":
            call help
          "ADD":
            result = a_v1 + a_v2
          "SUB":
            result = a_v1 - a_v2
          "MUL":
            result = a_v1 * a_v2
          "DIV":
            result = a_v1 / a_v2
          endcase
        else
          writes(TTCHN, "...Invalid option")
      end
done,
    freturn result

help,
    xcall math_help
endfunction