You are here: Core Reference > Coding > Logic > IF - THEN - ELSE

IF - THEN - ELSE

The IF-THEN-ELSE statement enables you to execute different Logic statements based on the value of an input, for example, to control the value of an item based on the value of an input item.

Example:

This is a simple control of an output:

  • IF Level < 75 THEN
    • Pump1 := FALSE;
    • Pump2 := FALSE;
  • ELSE
    • Pump1 := TRUE;
    • Pump2 := TRUE;
  • END_IF;

In this Logic, the Pump1 state and Pump2 state are set to OFF (FALSE) when the value of the Level is less than 75. When the value of the level is not less than 75 (in other words, it is greater than or equal to 75), the Pump1 and Pump2 states are set to ON (TRUE). This type of Logic can be used for an application in which a pump needs to be turned on to pump liquid out of a pipe when the level rises to 75.

You need to enter IF-THEN-ELSE statements in the following format:

You can enter multiple statements into the branches of an IF-THEN-ELSE statement. This enables you to have blocks of ST code that are executed or ignored depending on an expression:

You can nest IF-THEN-ELSE statements to create more complex control Logic.

Example:

  • IF PumpQuality = Good THEN
    • IF Valve = CLOSED THEN
      • Valve := OPEN;
    • END_IF;
  • ELSE
    • PumpQuality := Bad;
  • END_IF;

In this example, the parent IF-THEN-ELSE statement instructs the program to evaluate the nested IF-THEN-ELSE statement when the PumpQuality is Good. If the PumpQuality is Bad, the program will not evaluate the nested statement.

The nested IF-THEN-ELSE statement is only executed when the PumpQuality is Good. It instructs the program to check if the valve is closed. If it is closed, then the program will open the valve.

When you nest an IF-THEN-ELSE statement, you need to apply the same formatting rules. The statement needs to be started with an IF keyword and has to be ended with an END_IF; keyword.

NOTE: We recommend that you indent nested IF-THEN-ELSE statements as this makes the program easier to read.

For a related statement, see IF - THEN - ELSIF - ELSE


ClearSCADA 2015 R2