CASE_OF

Use CASE_OF to select what to do based on a numerical value.
Operands
CASE numeric_expression OF
selector1: statement;
selectorN: statement; ELSE
Structured Text
Operand
Type
Format
Enter
Numeric_
expression
SINT INT DINT REAL
Tag expression
Tag or expression that evaluates to a number
(numeric expression)
Selector
SINT INT DINT REAL
Immediate
Same type as numeric_expression
IMPORTANT:
If using REAL values, use a range of values for a selector because a REAL value is more likely to be within a range of values than an exact match of one, specific value.
Description
The syntax is described in the table.
CASE_OF
These are the syntax for entering the selector values.
When selector is
Enter
One value
value: statement
Multiple, distinct values
value1, value2, valueN : <statement>
Use a comma (,) to separate each value.
A range of values
value1..valueN : <statement>
Use two periods (..) to identify the range.
Distinct values plus a range of values
valuea, valueb, value1..valueN : <statement>
The CASE construct is similar to a switch statement in the C or C++ programming languages. With the CASE construct, the controller executes only the statements that associated with the first matching selector value. Execution always breaks after the statements of that selector and goes to the END_CASE statement.
Affects Math Status Flags
No
Major/Minor Faults
None
Example
If you want this
Enter this structured text
If recipe number = 1 then Ingredient A outlet 1 = open (1) Ingredient B outlet 4 = open (1)
CASE recipe_number OF
1:
Ingredient_A.Outlet_1 :=1; Ingredient_B.Outlet_4 :=1;
If recipe number = 2 or 3 then
Ingredient A outlet 4 = open (1)
Ingredient B outlet 2 = open (1)
2,3:
Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1;
If recipe number = 4, 5, 6, or 7 then Ingredient A outlet 4 = open (1) Ingredient B outlet 2 = open (1)
4..7: Ingredient_A.Outlet_4 :=1; Ingredient_B.Outlet_2 :=1;
If recipe number = 8, 11, 12, or 13 then Ingredient A outlet 1 = open (1) Ingredient B outlet 4 = open (1)
8,11..13
Ingredient_A.Outlet_1 :=1; Ingredient_B.Outlet_4 :=1;
Otherwise all outlets = closed (0)
ELSE
Ingredient_A.Outlet_1 [:=]0; Ingredient_A.Outlet_4 [:=]0; Ingredient_B.Outlet_2 [:=]0; Ingredient_B.Outlet_4 [:=]0;
END_CASE;
The [:=] tells the controller to also clear the outlet tags whenever the controller does the following:
Enters the RUN mode.
Leaves the step of an SFC if configuring the SFC for Automatic reset. This applies only embedding the assignment in the action of the step or using the action to call a structured text routine via a JSR instruction.
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.