PLC short circuit AND_THEN evaluation guard - PLC Playground troubleshooting guide

Your PLC Guard Condition Is True but the Expression Still Faults? Check Evaluation

A guard expression protects a later operation only if evaluation rules actually skip that operation. Do not assume an ordinary AND short-circuits on every PLC platform. Read the language rules before relying on it to protect an index or divisor.

Short-circuit support is language- and target-specific.

What is happening?

Some environments provide separate short-circuit operators. CODESYS documents AND_THEN for supported Boolean operands. Ordinary evaluation, function side effects, and compiler target behavior still need careful review.

Check these five things

  1. Identify the potentially invalid operation.

  2. Read evaluation rules for the chosen operator.

  3. Use explicit conditional structure or a documented supported mechanism.

  4. Avoid hiding required updates inside condition operands.

  5. Test both rejected and accepted paths offline.

Worked example

A teaching condition first checks whether an index is valid and then reads an array element. If the second expression is evaluated regardless of the first result, the guard has not prevented the invalid access. Explicitly structured validation makes that dependency easier to review.

Quick diagnostic reference

Question Purpose
Operand evaluated? Guard actually protects
Function has side effects? Skipping changes behavior
Target supports operator? Portable implementation

The mistake to avoid

Replacing every AND with a short-circuit variant can skip function calls that previously ran. Review semantics rather than applying a mechanical global replacement.

How to verify the fix

Use invalid boundary inputs and observe both the final result and which operations execute. Confirm no required state update depends on an accidentally skipped operand.

Field-work boundary: These are educational diagnostic guides. Use the exact equipment manuals and approved site procedures. Electrical testing and machinery changes belong to qualified, authorized personnel; control hazardous energy and validate affected functions before release.

Technical reference

CODESYS: AND_THEN operator. The examples and diagnostic tables above are original teaching material; they do not replace the product manual.

Continue troubleshooting

Previous Post