Skip to content

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.

Lesson 9 of 3410 minIEC 61131-3

Assignment is :=, not =

ST has two separate symbols:

Symbol

Meaning

Example

:=

assignment: put the value into the variable

Pump := TRUE;

=

comparison: are the two values equal?

IF State = 3 THEN

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 ( ), function calls

2

NOT, unary minus

3

* / MOD

4

+ -

5

< > <= >=

6

= <>

7

AND

8

XOR

9

OR

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_IF

Try 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

  1. Run the program and compare IntHalf (3) with RealHalf (3.5) in the watch table.
  2. Compare NoParens (14) with Parens (20): the brackets changed the result.
  3. 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

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers