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:

IF <item name or value name> = <value> THEN

<Value name> := <specific value>;

ELSE

<Value name> := <specific value>;

END_IF;

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:

IF expression THEN

statement 1a;

statement 1b;

statement 1c;

....and so on...

ELSE

statement 2a;

statement 2b;

statement 2c;

....and so on...

END_IF;

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.

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


Disclaimer

Geo SCADA Expert 2019