Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

AND_THEN / OR_ELSE Used With Non-Boolean Operands

The AND_THEN and OR_ELSE operators provide explicit short-circuit evaluation semantics and are only meaningful for boolean operands. When used with integer types, short-circuit evaluation does not apply — use AND / OR instead for bitwise operations.

Erroneous code example:

PROGRAM main
VAR
    a : DINT;
    b : DINT;
    c : DINT;
END_VAR
    c := a AND_THEN b;  // ❌ AND_THEN requires BOOL operands
    c := a OR_ELSE b;   // ❌ OR_ELSE requires BOOL operands
END_PROGRAM

To fix, either use boolean operands:

PROGRAM main
VAR
    a : BOOL;
    b : BOOL;
    c : BOOL;
END_VAR
    c := a AND_THEN b;  // ✅ correct: both operands are BOOL
    c := a OR_ELSE b;   // ✅ correct: both operands are BOOL
END_PROGRAM

Or use AND / OR for bitwise operations on integers:

PROGRAM main
VAR
    a : DINT;
    b : DINT;
    c : DINT;
END_VAR
    c := a AND b;  // ✅ correct: bitwise AND
    c := a OR b;   // ✅ correct: bitwise OR
END_PROGRAM