Structured text is a very powerful language in PLC programming. If you are able to write complex statements in it, then you can execute even a very large system smoothly. One such statement is while-do. The While-Do statement is used to perform repetitive tasks with minimal coding. Such powerful tools minimize the size of your program and save a lot of memory. In this post, we will see a case for writing this while-do statement in the PLC program.
What is a while-do statement in structured text?
A while-do statement is a construct in structured text language, which is used to perform repetitive tasks. It checks a condition and as long as the condition is true, the statements written under this construct are executed. These statements are useful when there is a task which needs to be done continuously as long as the condition is true. Once the condition becomes false, the construct stops executing.
The statement looks like this as shown below. As long as the expression is true, the three statements will execute. When the expression becomes false, then they won’t execute.
while expression do
statement-1;
statement-2;
statement-3;
exit;
end_while;
There are some general points to consider when writing while-do statements:
- As this construct is already working for a loop, do not create loops under a single loop. The PLC CPU will consider it then as an infinite loop which cannot be executed, and will go under fault mode if the watchdog timer elapses.
- Whenever writing this statement, a way must be found to come out of the loop every once. For this, a syntax called exit is used. Or, the statement must be written in such a way that it comes out of the construct and executes the loop once again. If the processing is caught inside only, then the PLC will go into fault mode.
- PLC consumes some larger scan time when executing these instructions, so avoid using it in many instances. It will then be possible that some important logic may be skipped by the CPU to execute. So, take care in writing these statements effectively and in a limited way.
- Exit is not compulsory to use. But if not using, then there must be some other way written in your statement to jump out of the loop. But practically, exit is preferred to write because it simplifies your problem.
PLC Program using while-do statement
Let us understand the case first. We are decrementing a number by 1 every 1 second, starting from 10. When that number reaches 0, it will again start decrementing from 10. The loop goes on in this fashion.
Refer to the below image for the program written.
![While Do Statement in Structured Text PLC Program](https://instrumentationtools.com/wp-content/uploads/2025/02/While-Do-Statement-in-Structured-Text-PLC-Program.png)
We will have a detailed study exploring it more in the below image. In the first line, as long as the secs value is more than zero, the loop will start to execute. In the second line, we start the loop by first defining a timer – sec_trigger.
In the third line, we enable the timer by inserting the NOT function of it’s own done bit. Due to this, the timer will run continuously every one second. In the fourth line, we move the timer setpoint as 1 second. In the fifth line, if the timer is done, then we self-decrement the value by 1.
In the sixth line, the loop is exited. In the seventh line, the while loop is ended. But internally, it will jump again to the while statement. In the eighth line, if the value is less than or equal to zero, then we move 10 again in the variable.
So practically, the loop executes from 10 to 0 by decrementing itself, and once zero is reached, 10 is moved and the decrement starts again. If the last line was written inside the loop, then it would not have come out of the loop, and the PLC would have gone into fault mode.
This means, the basic rule is simple – execute the loop once, come out of it and execute again if the condition is ok. Or if you do not wish to continue after your work has been done, then there is no need to write the last line. The decrement will then happen only till 0 and the loop will stop executing after it. So, it all depends on your requirements.
![PLC Program using while-do statement](https://instrumentationtools.com/wp-content/uploads/2025/02/PLC-Program-using-while-do-statement.png)
In this way, we saw how to write a while-do statement in the PLC program.
Read Next:
- Simple Conveyor Control PLC Program Example
- Anti-static Wrist Straps in Industrial Automation
- PLC Traffic Light Sequence using Functional Blocks
- Structured Text PLC Sequential Process Data Storage
- Increment and Decrement Instructions in OMRON