Skip to main content

ST Statement: IF

The IF statement is used for checking a condition and, depending on this condition, for executing the subsequent statements.

A condition is coded as an expression that returns a Boolean value. If the expression returns TRUE, then the condition is fulfilled and the corresponding statements are executed after THEN. If the expression returns FALSE, then the following conditions, which are identified with ELSIF, are evaluated. If an ELSIF condition returns TRUE, then the statements are executed after the corresponding THEN. If all conditions return FALSE, then the statements are executed after ELSE.

Therefore, at most one branch of the IF statement is executed. ELSIF branches and the ELSE branch are optional.

Syntax

IF <condition> THEN
    <statements>
( ELSIF <condition> THEN
            <statements> )*
( ELSE
            <statements> )?
END_IF;
// ( ... )* None, once or several times
// ( ... )? Optional
Example 70. Example
PROGRAM PLC_PRG
VAR
        iTemp: INT;
        xHeatingOn: BOOL;
        xOpenWindow: BOOL;
END_VAR

IF iTemp < 17 THEN
        xHeatingOn := TRUE;
ELSIF iTemp > 25 THEN
        xOpenWindow := TRUE;
ELSE xHeatingOn := FALSE;
END_IF;

The program is run as follows at runtime:

For the evaluation of the expression iTemp < 17 = TRUE, the subsequent statement is executed and the heating is switched on. For the evaluation of the expression iTemp < 17 = FALSE, the subsequent ELSIF condition iTemp > 25 is evaluated. If this is true, then the statements in ELSIF are executed and the view is opened. If all conditions are FALSE, then the statement is executed in ELSE and the heating is switched off.