09Expressions and operators
Assignment and arithmetic in ST: := and operator precedence
The difference between := and =, the five arithmetic operators, precedence, and the most common bug in PLC maths: integer division.
Assignment is :=, not =
ST has two separate symbols:
Symbol | Meaning | Example |
|---|---|---|
| assignment: put the value into the variable | |
| comparison: are the two values equal? | |
Every assignment ends with a semicolon ;. A missing semicolon is the first compile error everyone meets.
Litres := Level * 25.0; (* assignment *)
Lamp := Level = 50.0; (* assigning the result of a comparison, perfectly valid *)The arithmetic operators
+ add, - subtract, * multiply, / divide, and MOD for the remainder.
Total := A + B;
Remaining := Capacity - Total;
Volume := Level * 25.0;
Average := Total / 4.0;
Phase := Counter MOD 3; (* gives 0, 1, 2, 0, ... *)MOD is more useful than it looks: splitting work across three scans, dividing down a scan counter, blinking a lamp.
Precedence
Operators are not evaluated left to right. They follow a fixed precedence, highest first:
Level | Operators |
|---|---|
1 | parentheses |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
That is why this line works without brackets around the comparison:
Pump := Start AND Level < 90.0;< binds tighter than AND. Even so, add the parentheses. A line you have to read twice is a bad line, correct or not.
The trap: integer division
If both operands are integers, the result is an integer and the fraction is truncated, not rounded:
IntHalf := 7 / 2; (* 3, not 3.5 and not 4 *)
RealHalf := 7.0 / 2.0; (* 3.5 *)The classic version of this bug lives in scaling code:
Percent := Raw / 27648 * 100; (* wrong: 0 for any Raw below 27648 *)
Percent := Raw * 100.0 / 27648.0; (* right: multiply first, use reals *)The rule: if the result is a ratio or a fraction, make at least one operand REAL, and multiply before you divide.
Division by zero
On most controllers a division by zero faults the program. Guard the denominator:
IF Span > 0.0 THEN
Percent := (Raw - Zero) * 100.0 / Span;
END_IFTry it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program and compare IntHalf (3) with RealHalf (3.5) in the watch table.
- Compare NoParens (14) with Parens (20): the brackets changed the result.
- Press START and watch Rate: the lamp lights while the fill rate is above 100 litres a minute.
Terms in this lesson
People find this lesson by searching
structured text assignment operatorst operator precedenceinteger division plcMOD structured textassignment vs comparison stplc scaling formulaINT vs REAL division
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers