×

Subscribe to newsletter

subscribe to our weekly newsletter to get notified with latest story and article Subscribe now!




PLC BLOG | PLC programming - Structured Text For Do Loop
Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


PLC programming - Structured Text For Do Loop

In programming For_Do loop is a conditional statement used to repeatedly execute a block of code for a specific number of times or until a certain condition is met. It is used in array, UDT etc. For_Do loop not used to many time in a single scan because it causes controller watchdog to timeout and causes a major fault and controller does not execute other statements in the routine until it completes the loop.


Structure Text For_Do Loop Syntax

syntax for For_Do loop is below
  • FOR count:= initial_value TO final_value
  • BY increment
  • DO
  • main_statement;
  • END_FOR;

  • count is a variable that store initial value after each increment or decrement. data type is integer type (DINT,UDINT)
  • initial_value : it is starting value
  • final value: last value or final value of the loop.
  • increment value: increment or decrement intitial value and stop decrment when it reached final value. it is any constant or any integer value.
  • main_statement: this statement execute until count reached to final value. every increment statement is executed.

Loop execute main_statement from initial_value to final_value. The initial value is the starting point of the loop and Final value is the end point. Initial value reach to final_value by increment or decrement as defined in loop.

structure text For_Do loop syntax

Example Of Structured Text For_Do Loop

in this example when for loop executed than value 10 stored in array of indexed 0 to 10.

Example Of Structured text For_Do Loop

in second example of level comparison. Conditional statement if else is used if limit switch 1 is operated than 10 lamp is on otherwise lamp remain off.

second Example Of Structured text For_Do Loop

in third example when limit switch is operated than all value of FOR_DO loop is set by predefined value. In this example value is increment by 2 you can increment of any value.

third Example Of Structured text For_Do Loop

in below code when for loop is executed than value of 0 to 10 is stored in array of counter_value[10].

fourth Example Of Structured text For_Do Loop

below is example of comparison of two array by using FOR_DO loop when both value is with same index is equal than value is stored in new tag. IF is used inside the For loop.

5 Example Of Structured text For_Do Loop

Decrement Of For_Do Loop Structured Text

for decrement of FOR_DO loop always remember initial value is greater than final_value. if you don’t do show when for loop is executed than your statement is not executed because of wrong condition of for loop and you also got a major fault error some time. so in decrement always make initial value larger than final value. Below is the example of decrement of For_Do loop, in this example when for loop is executed than value is stored in array of dec_value from 10 to 0.

decrement of For_Do Loop

Disadvantages Of Using A For_Do Loop

a for loop is that it requires you to know how many time statements requires to execute(iterations) you will need to perform before you start the loop. If you don't know the exact number of iterations in advance, you may end up either iterating too few or too many times, which can lead to errors in your program. Structure of For_Do is complex it have multiple conditions and expressions, making it harder to follow the flow of the loop. If you are working with large data sets. This is because the for loop must check the condition and increment the counter on each iteration, which can add up to a significant amount of overhead for large data sets.

Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


Suggested Post


 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

comment