FOR DO Statement in SCL Language

In the previous post, we saw how to write a case statement. In this post, we will see one of the most powerful statements in SCL language – for do statement. There are many repetitive tasks which need to be done in a PLC program. For this purpose, instead of writing statements multiple times, you can use for-do statements, where a repetitive task is performed more easily and in a very less code size. This helps the programmer to easily troubleshoot a system. Let us see what this statement is further in the post.

What is a For Loop statement?

A for loop statement is a construct which is used to perform a function when it is present between a defined condition. Once the condition goes false, the execution stops. The statement checks for a value between two set values (one initial and one final). It starts with an initial value and goes on incrementing or decrementing till the final value is reached. After that, it leaves the loop and goes on to execute next statements. The general syntax for this style is as below:

For variable := initial value To final value By Value Do

Statement;

End_for;

The meaning is – the initial value is moved to the variable, the statement executes and ends. When the loop is exited and occurs again, the variable is automatically incremented or decremented by the value mentioned (it is optional; if not mentioned, then increment or decrement automatically happens by 1). Then again, the statement is executed and ends. The loop goes on executing as the variable is added or subtracted on every loop exit, until the final value is reached. Once reached, the loop stops executing.

If it is required to exit the loop in between, then mention a keyword named exit; in between. Due to this, the loop terminates without executing fully. You can also write this under an if-else condition, to come out forcefully from the loop. One thing to remember is to not execute a loop under an already existing loop; otherwise it will become infinite and the PLC will go into fault mode.

How to write a For-Do statement?

Refer to the below image. The logic is – we need to add five values and store it in a destination. For this, the destination tag is sum, the tag where the values are stored for addition is item, which is an array of five elements, each having it’s own value. The tag index is used as a pointer to the item array element.

FOR DO Statement in SCL Language

The execution flow is as follows –

  1. We have fixed the five values of the item as 2,4,6,77,32.
  2. First, the sum is initiated to 0, so that abrupt addition does not execute.
  3. Then, the index is initiated to 0, from the condition written as 0 to 4, where 0 is moved to the index tag. So, the statement will be executed as reflecting the following – 0 := 0+2; which brings the answer as 2 (sum := sum+item[0]).
  4. The loop ends and moves again to the condition, where the value of index is now 1. So, the statement will execute as reflecting the following – 2 := 2+4; which brings the answer as 6 (2 is derived from the previous result and 4 is derived from the set value we have given; (sum := sum+item[1]).
  5. It then goes on adding till the value of index is 4 (sum := sum+item[14). Once the statement has been executed, the loop exits and stops executing. So, we get the final answer as a sum := =2+4+6+77+32, which is equal to 121.

The value of the index is incremented automatically by the PLC CPU scan time on each loop ending. It has nothing to be written separately by a programmer as shown. You could have also written this statement as:

sum := item[0] + item[1] + item[2] + item[3] + item[4] ;

But, this is good for our example of simple addition. If complex statements are required to be executed continuously in a loop, then you would have to write an if-else statement, which would make the program bulkier. In that case, a for loop statement makes a task much easier to work.

In the next post, we will see how to use a while loop statement in SCL language.

Read Next:

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

Leave a Comment

Share via
Follow us and never miss an update!