You are here: Core Reference > Coding > Logic > CASE - OF

CASE - OF

You can use the CASE - OF statement to set the values of defined variables based on an integer input. For example, you can use the CASE - OF statement to set the values of points named 'Point 2' and 'Point 3' based on the status value of a point named 'Point 1'. The 'Point 1' status is represented by an integer value that is used as an input.

When you enter a CASE - OF statement, you need to use the following format:

Where <Name of Integer Input> is the name of the integer variable that is used to determine the values of the other variables. The <Name of Integer Input> has to be defined in the VAR list for the program.

<Statements> represents the values that are set when the integer variable is the defined value. You can set multiple statements based on the integer variable being a defined value. For example, if Integer Value 2 is 6, you can define statements that set variable 3 to 90 and variable 4 to 15. The format for defining a statement is:

You can use the ELSE statement to define the values for the statements when the integer input is not a value included in the CASE - OF definition. For example, if an integer value has 6 possible settings and only settings 1 and 2 are used in the CASE - OF statement, the ELSE statement needs to be used to define the variable values when the integer value is 3, 4, 5, or 6. You need to end the CASE - OF statement with an END_CASE; keyword.

Example:

  • TYPE
    • FanCooler: (Low, High, Off);
  • END_TYPE
  • PROGRAM CoolerSpeed
  • VAR
    • Fan_Setting: INT;
    • SPEED: INT;
    • COOLER: FanCooler;
  • END_VAR
    • COOLER := Off;
    • CASE Fan_Setting OF
    • 1: SPEED := 5;
    • 2: SPEED := 10;
    • 3,4,5: SPEED := 20; COOLER := Low;
    • 6..8: SPEED := 40; COOLER := High;
  • ELSE
    • SPEED := 0;
  • END_CASE;
  • END_PROGRAM

In this example, the Logic is used to set the speed of a fan and its cooler based on the integer input of the fan.

An encapsulated data type is created to define the fan cooler settings. The data type is named FanCooler and it defines the three fan cooler values (Low, High, and Off).

At the start of the program, the COOLER variable is defined as Off. This means that the COOLER variable is set to Off unless set to be Low or High by the Logic.

The variables list contains 3 variables: Fan_Setting which represents the integer value that indicates the state of the fan, SPEED which represents the rate at which the fan is running, and COOLER which represents the fan cooler setting. The Fan_Setting variable is set to INT as it provides an integer value. This value is used by the Logic program to determine the values of the COOLER and SPEED variables. The SPEED variable is also an integer value (for the example, the values are internal values), and the data type for the COOLER variable is FanCooler (a reference to the encapsulated data type).

The CASE - OF statement is used to define how the Logic program sets the values for the variables:

When the Fan_Setting variable is 1, the program sets the SPEED variable to 5.

When the Fan_Setting variable is 2, the program sets the SPEED variable to 10.

When the Fan_Setting variable is 3, 4, or 5, the program sets the SPEED variable to 20 and the COOLER variable to Low.

When the Fan_Setting variable is 6 to 8 inclusive, the program sets the SPEED variable to 40 and the COOLER variable to High.

Other Fan_Setting variable values cause the program to set the SPEED variable to 0.


ClearSCADA 2015 R2