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.
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_IFELSIF 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_IFThe semicolon rules
Three rules clear up 90% of compile errors:
- Every statement inside a branch ends with
;. - No semicolon after
THEN, and none afterELSE. END_IFcloses the construct. The standard needs no semicolon after it, but several vendors (Siemens SCL among them) writeEND_IF;and the lab here accepts that too.
IF Start THEN (* no semicolon here *)
Pump := TRUE; (* semicolon here *)
END_IFNesting 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_IFThat 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_IFRule 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_IFThe 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
- Run the program, turn on S1, then S3: only L3 lights because S3 has the higher priority.
- Turn S4 on: the light moves to L4.
- Reverse the order so it starts with S1 and download again: now S1 always wins.
- 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
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers