Skip to main content

IF...THEN...ELSE Statements

The IF statement is the primary way to implement conditional logic in Structured Text. It allows the program to make decisions based on conditions that evaluate to TRUE or FALSE.

Basic IF Statements

A simple IF statement executes one or more statements only if a condition is true.

Syntax:

IF <condition> THEN
<statements>;
END_IF;

Example:

IF Start_Button THEN
Motor_Start := 1;
END_IF;

IF...THEN...ELSE

You can extend an IF statement with an ELSE block to handle the case where the condition is false.

Syntax:

IF <condition> THEN
<true_statements>;
ELSE
<false_statements>;
END_IF;

Example:

IF Start_Button THEN
Motor_Start := 1;
ELSE
Motor_Start := 0;
END_IF;

This structure ensures the motor starts only when the button is pressed, and stops otherwise.

IF...ELSIF...ELSE

For multiple conditions, use ELSIF (else-if). This is helpful for multi-way logic.

Syntax:

IF Mode = 1 THEN
Operation := 1;
ELSIF Mode = 2 THEN
Operation := 2;
ELSE
Operation := 0;
END_IF;

Each condition is evaluated in order, and only the first matching block is executed.

Notes

  • Every IF block must be closed with END_IF;

  • Each line inside the block must end with a semicolon

  • You can nest IF statements, but keep them readable

More Examples

// Turn on fan only if temperature exceeds limit
IF Temperature > Temp_Limit THEN
Fan := 1;
ELSE
Fan := 0;
END_IF;

// Set alarm mode based on pressure
IF Pressure > 100 THEN
Alarm_Mode := 2;
ELSIF Pressure < 30 THEN
Alarm_Mode := 1;
ELSE
Alarm_Mode := 0;
END_IF;