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 is an extension; without the
--allow-bit-string-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.
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