ST Instruction ‘IF’

The IF instruction is used to check a condition and, depending on this condition, to execute instructions.

Syntax:

IF <boolean expression_1>  THEN
 <IF-instructions>
 {ELSIF <boolean expression_2> THEN
 <ELSIF-instruction_1>
 .
 .
 ELSIF <boolean expression_n> THEN
 <ELSIF_instruction_n-1>
 ELSE
 <ELSE_instructions>}
 END_IF;

The section inside the curly parentheses {} is optional.

If <boolean expression_1> returns TRUE, CODESYS executes only the <IF_instructions> and none of the other instructions.

Otherwise CODESYS checks the boolean expressions in succession, starting with <boolean expression_2, until an expression returns TRUE. Subsequently, CODESYS evaluates all instructions located between this expression and the next ELSE or ELSIF instruction and executes them accordingly.

If none of the boolean expressions returns TRUE, CODESYS evaluates only the <ELSE instructions>.

Example

IF temp < 17
THEN heating_on := TRUE;
ELSIF temp > 25
THEN open_window := TRUE;
ELSE heating_on := FALSE;
END_IF;

See also