while_do
Use the while_do loop to continue performing an action while certain conditions are
true.
Operands
while bool_expression do
<statement>;
Structured Text
Operand | Type | Format | Description |
---|---|---|---|
bool_expression | BOOL | tag expression | BOOL tag or expression that evaluates to a BOOL value |
IMPORTANT:
- Do not iterate within the loop too many times in a single scan.
- The controller does not execute any other statements in the routine until it completes the loop.
- A major fault occurs when completing the loop takes longer than the watchdog timer for the task.
- Consider using a different construct, such as if_then.
Description
The syntax is:

The following diagrams illustrate how a while_do loop executes, and how an exit statement
leaves the loop early.
![]() | ![]() |
While the bool_expression is true, the controller executes only the statements within the while_do loop. | To stop the loop before the conditions are true, use an exit statement. |
Affects Math Status Flags
No
Fault Conditions
A major fault will occur if | Fault type | Fault code |
the construct loops too long | 6 | 1 |
Example 1
If performing the following, | Enter this structured text | |
The while_do loop evaluates its conditions first. If the conditions are true, the
controller then executes the statements within the loop. This differs from the repeat_until loop because the repeat_until loop executes
the statements in the construct and then determines if the conditions are true
before executing the statements again. The statements in a repeat_until loop are
always executed at least once. The statements in a while_do loop might never be
executed. | pos := 0; | |
While ((pos <= 100) and structarray[pos].value <> targetvalue)) do | ||
pos := pos + 2; | ||
Tag.DATA[pos] := SINT_array[pos]; | ||
end_while; |
Provide Feedback