Comparisons and IF / ELSIF / ELSE decisions in Structured Text
The Core Idea
A comparison produces a TRUE/FALSE result from two values. An IF decision uses such a result to choose which statements to execute, with ELSIF and ELSE expressing alternative branches.
What you will learn
Compare numbers with
=,<>,<,<=,>, and>=, and use the TRUE/FALSE result.Use one
IF/ELSIF/ELSEdecision so exactly one branch runs.Put a value that sits exactly on a limit into the band the requirement intends.
Why it matters
Threshold decisions need clear boundaries and deliberate branch selection. Otherwise a value can receive conflicting indications or miss the intended action.
The problem
A parts washer heats its wash water. A transmitter reports the tank temperature, and the operator panel shows one of three lamps:
| Signal | Type | Meaning |
TEMP_C | REAL | Measured tank temperature, in °C. Starts at 80.0 °C. |
TEMP_LOW | BOOL | TRUE below 40.0 °C: too cold to wash. |
TEMP_OK | BOOL | TRUE from 40.0 °C up to, but not including, 70.0 °C. |
TEMP_HIGH | BOOL | TRUE at or above 70.0 °C. |
Exactly one lamp should be on at any temperature. The first program sets all three lamps FALSE, then gives each band its own IF. The lesson simulator scans every 100 ms.
See it go wrong
Now set Tank temp to 55.0 °C, then 30.0 °C, then exactly 70.0 °C. Note which temperatures light more than one lamp.
Fix it
Rewrite the three IFs as one IF / ELSIF / ELSE decision, so exactly one lamp is on at every temperature. The checks include the exact limits, 40.0 °C and 70.0 °C. Editing pauses the program, so press Run to watch your version live.
Passing the checks records Structured Text progress on the shared comparison concept.
What happened and why
The three bands overlap: every temperature at or above 70.0 °C is also at or above 40.0 °C. Separate IFs cannot say "only one of these". One IF / ELSIF / ELSE decision can, because the controller checks its conditions from the top and runs only the branch under the first TRUE one. ELSE runs when none is TRUE.
TEMP_C | >= 70.0 | >= 40.0 | Branch that runs | Lamp |
| 39.9 °C | FALSE | FALSE | ELSE | LOW |
| 40.0 °C | FALSE | TRUE | ELSIF | OK |
| 70.0 °C | TRUE | not checked | IF | HIGH |
| 80.0 °C | TRUE | not checked | IF | HIGH |
Branch order is priority. With >= limits, test the highest limit first. Clearing all three lamps above the decision makes a lamp turn off when the temperature leaves its band. The lamps do not flicker, because the controller updates its real outputs only after the whole program has run.
Comparisons give TRUE or FALSE
| Comparison | Asks | At TEMP_C = 70.0 |
TEMP_C = 70.0 | Equal? | TRUE |
TEMP_C <> 70.0 | Not equal? | FALSE |
TEMP_C > 70.0 | Greater than? | FALSE |
TEMP_C >= 70.0 | Greater than or equal? | TRUE |
TEMP_C < 70.0 | Less than? | FALSE |
TEMP_C <= 70.0 | Less than or equal? | TRUE |
The operator decides where the boundary falls: > leaves the limit out, and >= includes it. Write the operator from the requirement's words. "Above" is >, "at or above" is >=, and "below" is <.
Assignment is not equality
| You write | It means |
TEMP_HIGH := TEMP_C >= 70.0; | Store the comparison's answer in TEMP_HIGH. |
TEMP_C = 70.0 | Ask whether two values are equal. The answer is TRUE or FALSE, and no variable changes. |
IF TEMP_C := 70.0 THEN | Not valid. The editor reports Expected 'THEN' after IF condition. |
Structured Text uses a single = for equality. == is not an operator here.
Common mistakes
Separate
IFs for overlapping bands: each one runs, so two lamps can light. Use oneIF/ELSIF/ELSE, or write conditions that cannot both be TRUE.Testing the lower limit first:
IF TEMP_C >= 40.0also catches 80.0 °C, so the HIGH branch never runs. Test from the highest limit down.>where the requirement says "at or above": the lamp is wrong only at the limit itself, which a quick test can miss. Test exactly at, just below, and just above each limit.Never clearing the lamps: a lamp stays on after the temperature leaves its band. Clear them first, or give every lamp a value in every branch.
Using
=on a measurement: a temperature can step from 69.8 °C to 70.3 °C without ever equaling 70.0 °C. Compare measurements with>=or<.Writing
:=in a condition, orELSE IFforELSIF: a condition only asks, so:=is an editor error.ELSIFis one word;ELSE IFopens a secondIFthat needs its ownEND_IF.
Platform notes
IEC 61131-3 (the international standard for programmable controller languages) also defines the comparisons as functions, such as
GE(TEMP_C, 70.0), and some editors offer them in function-block diagrams. This simulator uses the operators shown in this lesson.Some code-analysis tools warn when a
REALis compared with=or<>. The advice is the same as here: compare a measurement with a limit, not an exact value.
Enable JavaScript to run the embedded simulator and complete the interactive exercises.