Inst ToolsInst ToolsInst Tools
  • Courses
  • Automation
    • PLC
    • Control System
    • Safety System
    • Communication
    • Fire & Gas System
  • Instrumentation
    • Design
    • Pressure
    • Temperature
    • Flow
    • Level
    • Vibration
    • Analyzer
    • Control Valve
    • Switch
    • Calibration
    • Erection & Commissioning
  • Interview
    • Instrumentation
    • Electrical
    • Electronics
    • Practical
  • Q&A
    • Instrumentation
    • Control System
    • Electrical
    • Electronics
    • Analog Electronics
    • Digital Electronics
    • Power Electronics
    • Microprocessor
  • Request
Search
  • Books
  • Software
  • Projects
  • Process
  • Tools
  • Basics
  • Formula
  • Power Plant
  • Root Cause Analysis
  • Electrical Basics
  • Animation
  • Standards
  • 4-20 mA Course
  • Siemens PLC Course
Reading: While Do Statement in Structured Text PLC Program
Share
Font ResizerAa
Inst ToolsInst Tools
Font ResizerAa
  • Courses
  • Design
  • PLC
  • Interview
  • Control System
Search
  • Courses
  • Automation
    • PLC
    • Control System
    • Safety System
    • Communication
    • Fire & Gas System
  • Instrumentation
    • Design
    • Pressure
    • Temperature
    • Flow
    • Level
    • Vibration
    • Analyzer
    • Control Valve
    • Switch
    • Calibration
    • Erection & Commissioning
  • Interview
    • Instrumentation
    • Electrical
    • Electronics
    • Practical
  • Q&A
    • Instrumentation
    • Control System
    • Electrical
    • Electronics
    • Analog Electronics
    • Digital Electronics
    • Power Electronics
    • Microprocessor
  • Request
Follow US
All rights reserved. Reproduction in whole or in part without written permission is prohibited.
Inst Tools > Blog > PLC Tutorials > While Do Statement in Structured Text PLC Program

While Do Statement in Structured Text PLC Program

Learn how to use the powerful while-do statement in structured text language for PLC programming to execute repetitive tasks.

Last updated: January 7, 2025 3:19 pm
Viral Nagda
PLC Tutorials
No Comments
Share
6 Min Read
SHARE

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.

Contents
What is a while-do statement in structured text?PLC Program using while-do statement

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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

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

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
Don't Miss Our Updates
Be the first to get exclusive content straight to your email.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address
You've successfully subscribed !

Continue Reading

Top PLC Manufacturers
How to Design a System Architecture in Industrial Automation?
Converter Instruction in Siemens PLC Programming
PLC Program for Automatic Liquid Mixing Application
PLC Interview Questions and Answers
#20 PLC Best Practices – Identify Critical Alerts
Share This Article
Facebook Whatsapp Whatsapp LinkedIn Copy Link
Share
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

128.3kFollowersLike
69.1kFollowersFollow
210kSubscribersSubscribe
38kFollowersFollow

Categories

Explore More

Types of Limit Switches – Principle, Advantages, Disadvantages
How to Download GX Works? Mitsubishi PLC Software
#8 PLC Best Practices – Validate HMI Input Variables at PLC Level
Which Language is Best for PLC Programming?
What is Thin Client in SCADA? – Purpose, Advantages
Hardwired I/O and Serial I/O – Differences Explained
Design a PLC program for Operational Sequence of Packaging Process
Ladder Logic Example with Timers

Keep Learning

PLC Motor Logic

Interconnection between PLC, MCC, LCP & Pumps – Video

Light ON OFF Control using PLC

Light ON OFF Control using PLC

WHILE DO Statement in SCL Language

WHILE DO Statement in SCL Language

Unspecified CPU in Tia Portal

How to Add Unspecified CPU in Tia Portal?

PLC automation timers and counters

Medium-Level PLC Exercise for Students in Automation

ControlLogix Architecture - Rockwell Automation

ControlLogix Architecture – Rockwell Automation

Perfume filling system PLC program

CX Programmer Example: Perfume Mixing and Filling System

How to Design a Motor Faceplate in Graphics - SCADA Tutorials

How to Design a Motor Faceplate in Graphics? – SCADA Tutorials

Learn More

How to Use Loop Calibrator

Troubleshooting with Loop Calibrator

NACE Standards - MR0103 Vs MR0175

NACE Standards – MR0103 Vs MR0175

What is Static Grounding

What is Static Grounding?

Open Tank DP Level Measurement

Open Tank DP Level Transmitter Calculations

Zener Barrier Protection

Zener Diode Barrier Principle

Difference Between MCB and MCCB

Difference Between MCB and MCCB

Intrinsic semiconductors

Introduction to Semiconductor Electronics – Types, Materials

Layer-2 and Layer-3 Network Switches

Layer-2 and Layer-3 Network Switches

Menu

  • About
  • Privacy Policy
  • Copyright

Quick Links

  • Learn PLC
  • Helping Hand
  • Part Time Job

YouTube Subscribe

Follow US
All rights reserved. Reproduction in whole or in part without written permission is prohibited.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?