The IF statement is ST's most-used branch. The syntax is easy; the traps are in the details: confusing assignment with comparison, short-circuit evaluation side effects, floating-point equality, and runaway nesting. This article gives the standard form, common variants and four avoidance rules — IFs written this way stay clean.
Points: ① the condition must be boolean (TRUE/FALSE), built from comparisons and logic; ② ELSIF (one word; not ELSE IF — most platforms accept both but standard style is ELSIF) chains short-circuit in order — once one matches, later ones are not evaluated; ③ ELSE is optional, but control logic should write it (an explicit "everything else" makes missed states visible); ④ END_IF closes every IF — one per nesting level.
| Operation | Syntax | Note |
|---|---|---|
| Comparison | =, <>, >, <, >=, <= | Assignment is :=, comparison is = — never mixed |
| AND / OR / NOT | AND, OR, NOT | Series contacts in Mitsubishi terms |
| Exclusive-or | XOR | Handy for two-way exclusivity |
| Combinations | (A AND B) OR C | Parentheses beat default precedence — always |
Booleans stand alone as conditions: IF AutoMode THEN, not IF AutoMode = TRUE THEN — the latter is verbose and one keystroke from an assignment typo. Negation: IF NOT FeedBack THEN.
| Trap | Symptom | Rule |
|---|---|---|
| := versus = swapped | Assignment inside a condition (most platforms refuse; some language versions slip through) or comparison typed as assignment | Compiler warnings fully on; := only in statement territory |
| Floating-point equality | IF Temp = 50.0 never fires | Tolerance band: ABS(Temp-50.0) < 0.5 |
| Relying on short-circuit | Expecting the AND's right side not to run (especially calls with side effects) | Side effects out of conditions; conditions stay pure |
| Over-nesting | Five END_IFs stacked — unchangeable | Two levels max; early RETURN or split conditions |
On short-circuiting: most platforms evaluate AND lazily (left FALSE skips the right) — a performance feature and a trap: put a side-effecting call (counting, flag-setting) inside a condition and it "sometimes runs, sometimes doesn't". The condition zone holds pure judgements only.
Ladder "contacts in series + coil" translates to IF + assignment:
Note the third form: a pure boolean expression assigned directly reads better than an IF — and its semantics is "assigned every scan" (following), matching a ladder coil; an IF wrapping only TRUE with no ELSE means "can only set" — different semantics. Translate ladder with direct assignment; use IF+SET thinking for state memory.
Multi-way branches (steps, modes, states) read better as CASE; IF is left for global aborts, cross-state common conditions and binary choices. IF and CASE each in their lane keep the state machine layered.
| Symptom | Cause | Fix |
|---|---|---|
| "Type mismatch" at compile | Non-boolean condition (e.g. IF X+1 THEN) | Add the comparison operator |
| Branch never entered | Floating-point = / inverted condition | Tolerance band / re-check the logic |
| Flag occasionally lost | Side-effecting call short-circuited on the AND's right | Side effects out of conditions |
| END_IF mismatch | Nesting without closing | Write END_IF immediately after IF, then fill in |
| ELSE branch misses states | No ELSE, coverage incomplete | Explicit ELSE as the control-logic fallback |
The difference between good and bad IFs is not syntax but condition design: conditions pure of side effects, booleans used bare, floating point in tolerance bands, nesting within two levels. Hold the four rules and ST's IF beats any ladder contact tower — the first sweetness of moving from ladder to ST.
IF/ELSIF/END_IF syntax and boolean operations follow IEC 61131-3; short-circuit evaluation behaviour follows each platform's language implementation notes (CODESYS/TwinCAT/AutoShop); floating-point comparison handling follows IEEE 754 and engineering tolerance practice.
Two self-checks per IF: does the condition carry side effects? does the ELSE explicitly cover the rest? Answering "yes" routinely keeps ST code solid.