Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Function Blocks and Structured Text

Structured Text Control Flow

Learn Structured Text control flow for PLCs: IF/ELSIF/ELSE, CASE state machines, FOR, WHILE and REPEAT loops, EXIT, arrays and the rule to keep loops short.

Control flow decides which code runs. Structured Text has the familiar tools: IF for decisions, CASE for multi-way choices (perfect for state machines), and FOR, WHILE and REPEAT for loops. One PLC-specific rule is added: because the whole program must finish inside one scan, loops must be short and must always end.

ST code is shown read-only here; every construct is mirrored by a runnable C stand-in so you can watch the behavior.

IF / ELSIF / ELSE

TEXT
IF Temp > 90.0 THEN
    Alarm := TRUE;
    Heater := FALSE;
ELSIF Temp < 60.0 THEN
    Heater := TRUE;
ELSE
    Alarm := FALSE;
END_IF;

The conditions are checked in order and the first true branch runs. Every IF ends with END_IF;. Remember that an output assigned inside an IF keeps its old value when no branch assigns it. In the example, Heater stays as it was if Temp is between 60 and 90 (a simple hysteresis band).

C Stand-in: heater with hysteresis (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

CASE: state machines

A CASE selects a branch by an integer or enumeration. It is the standard way to code a machine that moves through states.

TEXT
TYPE State : (Idle, Filling, Heating, Draining); END_TYPE

CASE Step OF
    Idle:
        IF StartCmd THEN Step := Filling; END_IF;
    Filling:
        FillValve := TRUE;
        IF LevelHigh THEN
            FillValve := FALSE;
            Step := Heating;
        END_IF;
    Heating:
        Heater := TRUE;
        IF TempReached THEN Heater := FALSE; Step := Draining; END_IF;
    Draining:
        DrainValve := TRUE;
        IF LevelLow THEN DrainValve := FALSE; Step := Idle; END_IF;
ELSE
    Step := Idle;          (* unknown value: recover safely *)
END_CASE;

State machine habits

One state at a time A single Step variable
Clear exits Each state has conditions
ELSE branch Recover from bad values

Name states, not numbers

1 / 2
C Stand-in: a CASE state machine (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

FOR loops

FOR repeats a fixed number of times, with an index variable.

TEXT
Total := 0.0;
FOR i := 1 TO 10 DO
    Total := Total + Readings[i];
END_FOR;
Average := Total / 10.0;

BY sets the step: FOR i := 10 TO 1 BY -1 DO.

C Stand-in: average of an array with a FOR loop (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

WHILE and REPEAT

  • WHILE cond DO ... END_WHILE; tests first and may run zero times.
  • REPEAT ... UNTIL cond END_REPEAT; runs at least once and tests at the end.
  • EXIT; leaves the innermost loop early; CONTINUE; (in newer editions) jumps to the next iteration.
TEXT
i := 1;
WHILE i <= 100 AND Data[i] <> Target DO
    i := i + 1;
END_WHILE;
C Stand-in: search with an early exit (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

A WHILE loop whose condition never becomes false freezes the scan until the watchdog faults the CPU. Always bound a loop with a counter or a fixed FOR range, and keep it short. Never wait for an input inside a loop: inputs only update between scans (see the scan cycle).

Do not wait inside a scan

In general-purpose languages you may write "wait until the sensor turns on". In a PLC, the input image cannot change during the scan, so that loop would never end. The PLC way is a state machine: check the condition once per scan and move on if it is not yet true, as in the CASE example.

Choosing: IF vs CASE vs loops

Situation Use
Two or three conditions IF / ELSIF
A state or mode number CASE
Same operation on array elements FOR
Unknown count, bounded by a condition WHILE with a hard limit

Next: User-defined function blocks and types.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is CASE a good fit for a machine sequence?

    Each state is a CASE label and the Step variable tracks the current one.
  2. 2. What happens if a WHILE loop never ends in a PLC?

    The program cannot finish its scan, so the watchdog timer trips.
  3. 3. In ST, an output assigned only inside an IF branch:

    Variables retain their value unless assigned; this can create hysteresis or an accidental latch.
  4. 4. Which loop always runs its body at least once?

    REPEAT tests its condition at the end, so the body runs once before the first check.

Summary

  • IF, CASE, FOR, WHILE and REPEAT are the control structures.
  • Model sequences as a CASE state machine; check conditions once per scan.
  • Keep loops short and bounded; never wait inside a loop.
  • Unassigned outputs keep their old value, so assign them on every path when you mean to.

Next: User-Defined Function Blocks and Types