CASE

The CASE statement selects one of several statement groups based on the value of an integer or enumeration expression.

IEC 61131-3

Section 3.3.2.3

Support

Supported

Syntax

CASE expression OF
    case_value { ',' case_value } ':' statement_list
    { case_value { ',' case_value } ':' statement_list }
[ ELSE
    statement_list ]
END_CASE ;

Each case_value is an integer literal, a subrange (low .. high), or an enumerated value. Multiple values can be listed separated by commas.

A hex, binary or octal label such as 16#D012, or a subrange label with such a bound (16#01..16#0F), is an extension; without the --allow-radix-case-labels flag it is reported as P4041.

Every integer label, and each bound of a subrange label, must be a value the selector’s type can hold, whatever radix it is written in. A label outside that range is reported as P2026.

The selector expression must be of an integer type, a subrange of one, or an enumeration type. A selector of any other type, such as REAL or WORD, is reported as P4053.

Each label must be a value of the selector’s type. An enumerated value that labels a CASE on an integer is reported as P4101, and one that is not a value of the selector’s enumeration is reported as P2006.

Description

The CASE statement evaluates the expression and compares it against each case value in order. When a match is found, the corresponding statement list executes and control passes to the statement after END_CASE. If no match is found and an ELSE clause is present, its statement list executes.

Unlike C-style switch statements, there is no fall-through between cases.

Example

PROGRAM main
    VAR
        command : INT := 2;
        motor_on : BOOL;
        alarm : BOOL;
    END_VAR

    CASE command OF
        0:
            motor_on := FALSE;
            alarm := FALSE;
        1:
            motor_on := TRUE;
        2, 3:
            motor_on := TRUE;
            alarm := TRUE;
    ELSE
        alarm := TRUE;
    END_CASE;
END_PROGRAM

See Also

  • IF — conditional branching