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
Notification Show More
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

PLC Interlock Logic with First Input Priority
Automatic Vacuum Cleaner Programming in Omron PLC
Peripheral Inputs and Outputs in Siemens PLC
Automatic Empty Bottle detection using PLC Logic
Structured Text PLC Code for Sequential Process Data Storage
Step-by-Step Guide to Choose Right Fuse for a Panel
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

Instrumentation Engineer Activities & Documents – Detail Design Phase
Real-Time Clock in Omron PLC? – CX Programmer
PLC Programming for Train Detection and Gate Operation
PLC Program for Alternate Output Circuit with Latched Function
Shutter Door Control using Motor and Limit Switches
PLC Motor Logic with START, STOP, TEST Push buttons
Test and Process Modes in Siemens PLC
Retentive ON Delay Timer using PLC

Keep Learning

How to Detect Speed of Conveyor in PLC

How to Detect Speed of Conveyor in PLC?

Difference Between Industrial Computer and Normal Computer

Difference Between Industrial Computer (IPC) and Normal Computer (PC)

Studio 5000 Logix Emulate

Simulation of Studio 5000 and FactoryTalk View Studio

Siemens TIA Portal Free Version Download

Siemens TIA Portal Free Version Download

PLC ON DELAY TIMER FUNCTION

#6 PLC Best Practices – Validate Timers and Counters

Siemens S7 300 and STEP 7 PLC program using SIMATIC Software

scada hmi

Functions of SCADA

PLC Sequential Lights Control Example

PLC Timer Example: Sequential Control of Three Lights with Reset

Learn More

How to Check the Continuity of Protective Conductors

How to Check the Continuity of Protective Conductors?

Difference between Electron and Proton

Difference between Electron and Proton

Introduction to GOC controllers

Mitsubishi Electric GOC Controllers

Digital Electronics Multiple Choice Questions

Parity Generators Objective Questions

Intrinsic Safety Barriers

Intrinsically Safe Barriers Questions and Answers

Classification of Weighing Balance

Classification of Weighing Balance and Weight

Calculate Hydraulic System Fluid Pressure

Calculate Hydraulic System Fluid Pressure

Magnetic Flow Meters

Magnetic Flow Meters Installation Guidelines

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?