WHILE loop
Syntax
WHILE <bool_expr> DO
<statement_list>
END_WHILE;
Semantics
- Evaluate
<bool_expr>. IfFALSE, skip the body and continue afterEND_WHILE. - Run the body.
- Go to step 1.
The body may run zero times if the condition is FALSE on
entry. This is the difference from REPEAT.
Examples
(* Drain a queue until empty *)
WHILE iQueueLen > 0 DO
ProcessOne();
iQueueLen := iQueueLen - 1;
END_WHILE;
(* Bounded poll *)
iSafetyCount := 0;
WHILE NOT xReady AND iSafetyCount < 50 DO
DoOneScan();
iSafetyCount := iSafetyCount + 1;
END_WHILE;
Scan-cycle hazard
WHILE without an explicit per-iteration safety counter is a
red flag in PLC code. Even if the body looks bounded, a
runtime data condition (sensor stuck, queue mis-counting)
can keep the loop running until the scan cycle is missed.
Always add an iSafetyCount and an upper bound that fits
into your task’s CPU budget.
IEC reference
IEC 61131-3 third edition (2013), clause 6.6.3.5.
matiec conformance
Implemented per the standard. The strict BOOL-condition rule is the main porting pain point from C/Pascal where any non-zero value is treated as truthy.