Combining logic and parentheses in Structured Text

The Core Idea

A combined logical expression makes one TRUE/FALSE decision from several conditions. Operator precedence determines how the expression is grouped; parentheses let you state the intended grouping explicitly.

What you will learn

  • Predict how Structured Text groups AND, OR, XOR, and NOT in one expression.

  • Use parentheses to make an expression match a written rule.

  • Check a combined condition against every combination of its inputs.

Why it matters

A control rule written naturally in words may be grouped differently by the language. Clear grouping keeps the program's decision aligned with the requirement.

The problem

A conveyor can be jogged from two stations. The rule: the conveyor jogs while either station's JOG button is held, and only while the guard is closed.

SignalTypeMeaning
JOG_ABOOLTRUE while the JOG button at station A is held. Starts held.
JOG_BBOOLTRUE while the JOG button at station B is held.
GUARD_CLOSEDBOOLTRUE while the conveyor guard is closed. Starts open.
CONVEYOR_JOGBOOLTRUE runs the conveyor at jog speed.

The program writes the rule in the order it is spoken: JOG_A OR JOG_B AND GUARD_CLOSED. The buttons on the human-machine interface (HMI) stay where you put them, so you can hold any combination. The lesson simulator scans every 100 ms.

This guard check is a control interlock. On a real machine, an independent safety circuit also stops the conveyor when the guard opens.

See it go wrong

Now release JOG A and hold JOG B with the guard still open. Then close the guard.

Fix it

Add parentheses so the line matches the rule: either station, and only with the guard closed. The checks try all eight combinations of the three inputs. Editing pauses the program, so press Run to watch your version live.

Passing the checks records Structured Text progress on the shared Boolean-expressions concept.

What happened and why

Here are both versions for every combination of the three inputs:

They differ only where JOG A is held and the guard is open. Without parentheses, the guard protected station B and never station A.

Structured Text does not read a line left to right. Each operator has a precedence, and higher-precedence operators are evaluated first, the way multiplication comes before addition in arithmetic:

EvaluatedOperatorsExample
First( )(JOG_A OR JOG_B)
NOTNOT GUARD_CLOSED applies only to GUARD_CLOSED
<, >, <=, >=, =, <>TEMP_C >= 70.0 AND PUMP_ON compares first
ANDJOG_B AND GUARD_CLOSED
XORTRUE when exactly one side is TRUE
LastOR

When a line mixes AND and OR, write the parentheses even where precedence happens to give the right answer. The next reader should not need this table.

In Ladder, the parentheses are the branch. JOG_A and JOG_B sit on parallel branches, and GUARD_CLOSED is in series after them. Without parentheses, GUARD_CLOSED sits on JOG_B's branch only.

Common mistakes

  • Reading the line left to right: JOG_A OR JOG_B AND GUARD_CLOSED means JOG_A OR (JOG_B AND GUARD_CLOSED). Group the condition the rule shares.

  • Grouping the wrong pair: (JOG_A AND GUARD_CLOSED) OR JOG_B guards station A and leaves station B unguarded. Check every input combination, not only the one you were thinking about.

  • Using XOR for "either": (JOG_A XOR JOG_B) AND GUARD_CLOSED stops the conveyor when both buttons are held. In a control requirement, "either" almost always means OR.

Platform notes

  • IEC 61131-3 (the international standard for programmable controller languages) also accepts & for AND. This simulator uses the keyword.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.