Skip to main content
Call Eric:863-698-8266
CURRYCONTROLS.COMControls & Automation Knowledge Hub
ReferencePLCProgrammingStandards

Structured Text

The text language of IEC 61131-3: syntax, control structures, how it fits alongside ladder, the conventions that keep it readable for the next person, and the mistakes that make structured text a liability in a plant that troubleshoots online.

10 min readUpdated Sep 5, 2026Published Sep 5, 2026By Eric Sullivan

The short answer

Structured Text

Structured text is the high-level text language defined by IEC 61131-3. It has assignments, IF and CASE branches, FOR and WHILE loops, and calls to functions and function blocks, and it is the right language for calculations, data handling, and logic with many conditions. It is harder to troubleshoot online than ladder, so in a water or wastewater program it is best used inside well-named function blocks that ladder calls, not as a replacement for the ladder an electrician expects to see.

Key points

  • Use it for arithmetic, data handling, state machines, and anything with a loop.
  • Keep discrete motor and valve control in ladder where it is troubleshot online.
  • One statement per line, named constants, and comments that say why, not what.
  • Every IF that writes an output has an ELSE that writes it too, or the output holds its last value forever.
  • A loop that can run long stalls the scan. Bound every loop.

What it looks like

Structured text reads like Pascal. Statements end in semicolons, assignment is a colon-equals, comparisons produce booleans, and blocks are delimited by keywords rather than braces. The example computes a scaled analog value with clamping and a validity flag, which in ladder would take a dozen rungs and in structured text takes a dozen lines that read top to bottom.

Scaling with validity, in structured text
(* Scale a 4-20 mA input to engineering units, with range checks. *)
Raw_Span := Raw_Max - Raw_Min;
IF Raw_Span > 0 THEN
    Level_ft := (Raw - Raw_Min) / Raw_Span * (EU_Max - EU_Min) + EU_Min;
ELSE
    Level_ft := EU_Min;            (* bad configuration: do not divide by zero *)
END_IF;

(* Under-range and over-range flags, with a small margin for calibration error. *)
Level_Under := Raw < (Raw_Min - Raw_Margin);
Level_Over  := Raw > (Raw_Max + Raw_Margin);
Level_OK    := NOT Level_Under AND NOT Level_Over AND Input_Card_OK;

(* Clamp the scaled value, but never let a clamped value look valid. *)
IF Level_ft < EU_Min THEN Level_ft := EU_Min; END_IF;
IF Level_ft > EU_Max THEN Level_ft := EU_Max; END_IF;

Every element of the language is visible: comments in parentheses and asterisks, assignment, arithmetic, IF with ELSE, comparisons producing booleans, and named variables in place of addresses.

Control structures

StructureUseCaution
IF ... ELSIF ... ELSE ... END_IFConditional assignmentWithout an ELSE, an output assigned only in the IF branch keeps its last value when the condition is false. Sometimes that is intended; usually it is a bug.
CASE ... OF ... END_CASESelection on an integer or enumeration; the natural form of a state machineInclude an ELSE branch that handles an unexpected value, and write the state to a monitored tag.
FOR ... TO ... DO ... END_FORIterate over an array: scan all pumps, all zones, all alarmsThe loop runs to completion inside one scan. A large or nested loop extends the scan time; a FOR loop in a fast task is a watchdog risk.
WHILE ... DO ... END_WHILE and REPEAT ... UNTILLoop until a conditionA condition that never becomes true stalls the scan and trips the watchdog. Bound them with a counter, or avoid them in controller code.
Function and function block callsCall a scaling function, a PID block, a pump control blockFunction block instances must be declared; calling one changes its stored state.

Where it belongs in a plant program

The question in a water or wastewater plant is not whether structured text is a good language; it is who will be standing in front of the panel at 2 a.m. An operator or an electrician troubleshooting online reads ladder: they see the contacts, the coils, and their states. Structured text shows them a page of code with values in the margin. That does not make structured text wrong; it makes it the wrong place for the logic they need to see.

  • Discrete control of motors, valves, and interlocks in ladder, calling function blocks whose internals may be structured text.
  • Analog scaling, validation, filtering, totalizing, and calculations in structured text inside function blocks.
  • State machines and sequences in structured text with a CASE statement, with the current state and step description written to tags the HMI shows.
  • Loops over arrays of equipment in structured text, never in ladder.
  • Communication message handling, string building, and data packing in structured text.

Conventions that keep it maintainable

  1. 01One statement per line. A line with three assignments hides two of them.
  2. 02Named constants for every number that is not zero or one. A 0.85 in the middle of a calculation is a question nobody can answer in five years.
  3. 03Comments that explain intent and units. The code already says what it does.
  4. 04Consistent naming: prefix by equipment, suffix by type, no abbreviations that only the author understands.
  5. 05Every IF that writes an output has an ELSE that writes it too, unless the hold is deliberate and commented.
  6. 06No magic addresses. Every variable is declared with a type and a meaning; the I/O mapping is in one place.
  7. 07Short blocks. A block longer than a screen is two blocks.
  8. 08Unit tests where the platform allows simulation, and at minimum a documented set of input cases with expected outputs.

Common mistakes

MistakeEffectFix
Output assigned only inside an IFOutput sticks at last value when the condition endsAdd the ELSE, or assign a default before the IF
Integer divisionFractions truncated silently; 7 / 2 = 3Convert to REAL before dividing
Comparing REALs for equalityAlmost never trueCompare against a tolerance
Unbounded WHILEScan stalls; watchdog faultUse FOR with a fixed bound, or a counter guard
State held in a local variable that is not retentiveState resets on power cycleDeclare it retentive, or reinitialize deliberately on first scan
Same output written from two placesLast write wins, and the first is invisibleOne owner per output
Logic that the HMI also doesTwo behaviors that drift apartLogic in the controller, display in the HMI

Frequently asked questions

Is structured text faster than ladder?
Execution speed is similar on most platforms; the compiler produces comparable code. Structured text is faster to write for calculations and slower to troubleshoot for discrete logic. Choose by readability for the person who will maintain it, not by execution speed.
Can I write the whole program in structured text?
You can, and some integrators do. In a utility where operators and electricians troubleshoot online, an all-text program is a maintenance problem they will resent for the life of the plant. Keep the discrete control they look at in ladder.
How do I troubleshoot structured text online?
Most platforms show live values beside each variable and let you set a breakpoint or a watch. The better answer is to write the block so it publishes its state, step, and hold reasons to tags, and to troubleshoot from those on the HMI or the tag monitor.
Does structured text move between platforms?
Better than any other language in the standard. The core syntax is the same; timers, function block declarations, and vendor extensions differ. Expect editing, not a rewrite.

Direct contact

Have a controls question?

Reach Eric Sullivan directly about anything on this site, a controls or automation topic, or one of his personal projects.