Skip to main content

ST Statement: CASE

The CASE statement is a conditional control structure which causes a statement or a section of a statement to be executed only under a specific condition. The section which is executed is determined by comparing the condition and label.

Syntax:

CASE <condition> OF
    <label-1> : 
        <instruction-1>
    <label-2> : 
        <instruction-2>
    <label-3, label-4, label-5> : 
        <instruction-3>
    <label-6 .. labe-10> : 
        <instruction-4>
    <label-n> : 
        <instruction-n>
    ELSE <ELSE-instruction>
END_CASE

condition

Integer variable for the condition

Example: iCondition

The value of the variables is compared with the labels declared in the construct.

Any number of labels (minimum: 2) can be used within a CASE statement. Otherwise, the construct can be displayed more clearly with an IF-THEN-ELSE construct.

All labels must have different values.

label-n

Constant, literal, or constant expression with the same data type as the condition

Acts as a label (jump target) within the CASE construct.

Example: 1, 5, c_ONE, c_TWO

If this value is equal to condition, then the following statements are run through.

If this value is not equal to condition, then the respective statement is ignored and the system jumps to the next label.

<label-n >, < label-n1>

Comma-separated list with multiple labels which act as jump targets

Example: 1, 5

If one of the labels matches the condition condition, then the following section is run through.

<label-n1>..<label-n2>

Range with lower and upper limit label

10..20

If the condition condition takes on a value from the range from label-n1 to label-n2, then the following section is run through.

ELSE

Optional, maximum once

Default jump target which is jumped to if all previous labels do not match the condition.

instruction-n

ELSE-instruction

Statement, or statement segment consisting of multiple statements

A statement always ends with a semicolon (;).

Example 72. Example
CASE iCondition OF
    1, 5, c_ONE, C_TWO: 
        bVar1 := TRUE;
        bVar3 := FALSE;
    2: 
        bVar2 := FALSE;
        bVar3 := TRUE;
    10..20: 
        bVar1 := TRUE;
        bVar3 := TRUE;
    ELSE
        bVar1 := NOT bVar1;
        bVar2 := bVar1 OR bVar2;
END_CASE