Navigation:  Programming > Structured Text > Concepts >

Conditionals

Previous pageReturn to chapter overviewNext page

Conditionals supported and specified by IEC-61131-3

wpshelp_common_fig_note

NOTE!

 

Check if equipment supports conditionals!

 

 

Conditional - IF

 

The IF statement allows a group of statements to be executed only if the associated boolean expression evaluates to true. If it evaluates to false, any subsequent conditions defined with ELSIF are checked. If any of these conditions are true, the corresponding statements within their THEN block are executed. If all conditions are false, the statements within the ELSE block, if present, are executed.

 

Therefore, at most only a single expression of the IF statement will be executed. The ELSIF and ELSE clauses are optional.

 

 

IF <boolean expression> THEN

 

   <instructions>

 

ELSIF <condition> THEN

 

   <instructions>

 

ELSE

 

   <instructions>

 

END_IF

 

 

 

Example

 

The example below demonstrates the logic for operating a cooling fan based on temperature conditions:

If the temperature is below 25°C, the fan remains off.
If the temperature is between 25°C and 40°C, the fan operates at medium speed.
If the temperature is higher than 40°C, the fan operates at high speed and an alarm is activated.

 

 

VAR

   alarm : BOOL;

   temperature : USINT;

   fanSpeed : USINT;

END_VAR

 

IF temperature < 25 THEN

   fanSpeed := 0;

   alarm := FALSE;

ELSIF temperature >= 25 AND temperature <= 40 THEN

   fanSpeed := 50;

   alarm := FALSE;

ELSE

   fanSpeed := 100;

   alarm := TRUE;

END_IF

 

 

Conditional - CASE

 

The CASE statement functions as a selector for groups of instructions, with each group identified by one or more literals, enumerated values, or subranges. When the selector's value matches a case, the corresponding group of statements is executed. If no match is found, the statements following the ELSE keyword (if present) are executed. Otherwise, no statements are executed.

 

The data types of these labels must match the data type of the variable specified in the selector.

 

 

 

CASE <expression> OF

 

   <label_1>

     

       <instructions>

 

   <label_2, label_3>

     

       <instructions>

 

   <label_4..label_7>

     

       <instructions>

 

   ELSE

 

       <instructions>

 

END_CASE

 

 

 

Example

 

The example below illustrates logic in which an input value is determined by the position of an eight-position selector switch:

 

VAR

   selectorSwitch : USINT;

   input : USINT;

END_VAR

 

 

CASE selectorSwitch OF

  0..3:

       input := 25;

   4:

       input := 35;

   5, 6:

       input := 40;

   ELSE

       input := 50;

END_CASE