Skip to content

12Control flow

IF, ELSIF and ELSE: the rules and when to avoid them

The full IF construct, the semicolon rules, nesting versus flattening, and the most common bad habit: using IF to assign a boolean.

Lesson 12 of 3410 minIEC 61131-3

The full construct

IF Level >= 90.0 THEN
  Band := 3;
ELSIF Level >= 60.0 THEN
  Band := 2;
ELSIF Level >= 20.0 THEN
  Band := 1;
ELSE
  Band := 0;
END_IF

ELSIF is one word, not ELSE IF. ELSE is optional. You can have as many ELSIF branches as you like.

The order is the logic

Branches are tested top to bottom, the first true condition wins, and execution jumps to END_IF. So the tightest test goes first. This code is broken:

IF Level >= 20.0 THEN
  Band := 1;              (* catches everything above 20% *)
ELSIF Level >= 90.0 THEN
  Band := 3;              (* can never run *)
END_IF

The semicolon rules

Three rules clear up 90% of compile errors:

  1. Every statement inside a branch ends with ;.
  2. No semicolon after THEN, and none after ELSE.
  3. END_IF closes the construct. The standard needs no semicolon after it, but several vendors (Siemens SCL among them) write END_IF; and the lab here accepts that too.
IF Start THEN        (* no semicolon here *)
  Pump := TRUE;      (* semicolon here *)
END_IF

Nesting versus flattening

You can put an IF inside an IF:

IF Auto THEN
  IF NOT Stop THEN
    IF Level < 90.0 THEN
      Pump := TRUE;
    END_IF
  END_IF
END_IF

That is a slope. Three levels today become seven next year and nobody will read them. Flatten with AND:

IF Auto AND NOT Stop AND Level < 90.0 THEN
  Pump := TRUE;
END_IF

Rule of thumb: never go past three levels of nesting. If you need more, the logic is asking for a CASE or for a split into smaller pieces.

When a plain assignment beats IF

This is the most useful idea in the lesson. If the outcome is a single boolean, do not use IF at all:

(* four lines, with a trap on the way *)
IF Level >= 90.0 THEN
  HiAlm := TRUE;
ELSE
  HiAlm := FALSE;
END_IF

(* one line, and clearer *)
HiAlm := Level >= 90.0;

The real trap is the forgotten ELSE:

IF Level >= 90.0 THEN
  HiAlm := TRUE;
END_IF

The alarm goes up and never comes down, because nothing brings it down and the variable keeps its value between scans. Every IF that raises an output needs something that clears it.

IF earns its place when you pick one of several paths, run a group of statements, or guard a calculation against a division by zero.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program, turn on S1, then S3: only L3 lights because S3 has the higher priority.
  2. Turn S4 on: the light moves to L4.
  3. Reverse the order so it starts with S1 and download again: now S1 always wins.
  4. Delete the ELSE branch: Level keeps its last value when every switch is off.

Terms in this lesson

People find this lesson by searching

IF statement structured textELSIF structured textEND_IF semicolonnested if plcplc conditional logicboolean assignment vs ifstructured text if else

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers