WHILE Statement

Help Contents

Introduction

The IEC 61131-3 ST WHILE statement executes a list of statements while a condition is true. The WHILE Statement has this syntax:

WHILE condition DO
    Statement;
    Statement;
    ..
    Statement;
END_WHILE;

Where:

If the condition is false when the WHILE Statement starts execution, the statements inside the loop will not be executed.

If the condition is true when the WHILE Statement starts execution, the statements inside the WHILE loop are executed until the condition becomes false.

To stop the WHILE loop early (before the condition becomes false) use the EXIT Statement.

While Loop Flow Diagram

The flow diagram of the IEC 61131-3 Structured Text While statement

The WHILE Loop uses this logic to execute statements:

  1. The condition (A) is evaluated.
  2. If the condition (A) is FALSE, statements (B) are skipped and the loop is complete.
  3. Otherwise, the condition (A) is TRUE and statements (B) inside the loop are executed in order.
  4. Execution continues with step 1.

Examples

  1. Use a WHILE loop to execute statements until a condition is FALSE:
    Index := 0;
    Sum := 0;
    WHILE Index < 10 DO
        Sum := Sum + Values[ Index ];
        Index := Index + 1;
    END_WHILE;

Standards Compliance

IEC 61131-3 Second Edition: Table 56.7.

IEC 61131-3 Third Edition: Table 72.7.

Further Information

REPEAT loop

For information about another loop type that uses a condition.

FOR loop

For information about another loop type that uses a fixed number of iterations.

Structured Text

For other Structured Text (ST) statement types.

Expressions

To learn about expressions that can be used as the condition in a WHILE loop.

Glossary

For the meaning of terms used in Fernhill SCADA.