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 / ELSE decision 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:

SignalTypeMeaning
TEMP_CREALMeasured tank temperature, in °C. Starts at 80.0 °C.
TEMP_LOWBOOLTRUE below 40.0 °C: too cold to wash.
TEMP_OKBOOLTRUE from 40.0 °C up to, but not including, 70.0 °C.
TEMP_HIGHBOOLTRUE 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.0Branch that runsLamp
39.9 °CFALSEFALSEELSELOW
40.0 °CFALSETRUEELSIFOK
70.0 °CTRUEnot checkedIFHIGH
80.0 °CTRUEnot checkedIFHIGH

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

ComparisonAsksAt TEMP_C = 70.0
TEMP_C = 70.0Equal?TRUE
TEMP_C <> 70.0Not equal?FALSE
TEMP_C > 70.0Greater than?FALSE
TEMP_C >= 70.0Greater than or equal?TRUE
TEMP_C < 70.0Less than?FALSE
TEMP_C <= 70.0Less 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 writeIt means
TEMP_HIGH := TEMP_C >= 70.0;Store the comparison's answer in TEMP_HIGH.
TEMP_C = 70.0Ask whether two values are equal. The answer is TRUE or FALSE, and no variable changes.
IF TEMP_C := 70.0 THENNot 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 one IF / ELSIF / ELSE, or write conditions that cannot both be TRUE.

  • Testing the lower limit first: IF TEMP_C >= 40.0 also 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, or ELSE IF for ELSIF: a condition only asks, so := is an editor error. ELSIF is one word; ELSE IF opens a second IF that needs its own END_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 REAL is 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.