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

One-Shot Rising and Falling Edge Instructions in Rockwell PLC
Various Types of Delta PLC used in Industrial Automation
PLC Motor Operation based on Time Cycle Sequence – Program
Example of Automated Guided Vehicle with PLC
Produced Tags and Consumed Tags in Studio 5000
PLC Ladder Logic for Start-up Control of Boilers
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

Tank Farm Management System: CX-Programmer [OMRON]
Ferrules and Cross Ferruling
Difference Between PLCSIM and PLCSIM Advanced? – Siemens PLC
Difference Between Microcontroller and PLC
Motor Feedback Fail Logic in PLC
Types of Interrupt OB in Siemens Tia Portal
PLC Program for Water Level Control Logic
PLC Valve Control Ladder Logic Programming

Keep Learning

PLC Program for Automatic Bottle Rejection System

PLC Program for Automatic Bottle Rejection System

Lights with Adjustable Timers PLC Programming

Flip-Flop PLC Programming: Lights with Adjustable Timers

How to Prevent Ground Loops in Analog Circuits?

How to Prevent Ground Loops in Analog Circuits?

Siemens PLC Math Instructions

Siemens PLC Mathematics Instructions

PLC Program for Automatic Parameter initialization

PLC Program for Automatic Parameter Initialization When Power UP

Organization block Siemens

Tia Portal – OB30 Cyclic Interrupt Organization Block

Web SCADA HMI

Web-based SCADA HMI Software

Downloading Free PLC software

Step by Step Guidelines for Downloading Free PLC software

Learn More

Overhead Product Pressure Control Loop

Overhead Product Pressure Control Loop

100 Instrumentation Questions

100 Instrumentation Basics Questions

What is a Pilot Valve?

What is a Pilot Valve? When do we use a Pilot Valve?

magnetic flow meter parts

What is a Magnetic Flow Meter?

Compressor Coolers

simple latching motor control circuit

Motor Control Circuit Wiring

Free Omron PLC Programming Course in HINDI

Free Omron PLC Programming Course in HINDI

What is a Magnetic Contactor

What is a Magnetic Contactor? – Principle, Types, Advantages

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?