Previously, we discussed the "case...of" statement, and in this session, we will delve into the "for...to...do" statement.
Explanation of the "for...to...do" Statement:
The for loop is a loop that allows you to specify the number of iterations. When using it, you first need to define an integer loop counter variable (TIA Portal V16 and later versions support unsigned integer data types). Then, specify the range for the loop. The for loop will automatically increment or decrement this loop counter variable according to your statement until the number of iterations exceeds the specified range, and the loop will terminate. The loop counter variable can be global, temporary, or static.
Example:
Define a temp=i=int
Define an output=DATA=ARRAY[0..4] OF INT
FOR #i := 0 TO 4 DO
#DATA[i] := i + 1;
END_FOR;
This is a basic for loop program that assigns values to the DATA array. Before execution, the for loop initializes i
to the starting value of 0, then increments it by 1 until i
equals 4. The statements within the loop are executed 5 times.
Based on the explanation, the final result of the above program is:
DATA[0] := 1 DATA[1] := 2 DATA[2] := 3 DATA[3] := 4 DATA[4] := 5
Someone might ask, "What if I don't want to increment by one step but by two steps instead?" It's possible.
Example:
Define a temp=i=int
Define an output=DATA=ARRAY[0..5] OF INT
FOR #i := 0 TO 5 BY 2 DO
#DATA[i] := i + 5;
END_FOR;
In this case, you can see that there is an additional "BY" clause. The number following "BY" specifies the step size for the loop. If omitted, the default step size is 1. If you specify "BY 2," then the loop will increment by two steps. The final data in the array will be:
DATA[0] = 5
DATA[2] = 7
DATA[4] = 9
Of course, you can increase the step size even more, such as BY 10, etc. This is just an example.
Now, since we have incrementation, is there decrementation as well? The answer is yes.
Example:
Define a temp=i=int
Define an output=DATA=ARRAY[0..5] OF INT
FOR #i := 5 TO 0 BY -1 DO
#DATA[i] := i - 5;
END_FOR;
To achieve decrementation, you only need to change the step size following "BY" to a negative number. In this case, the starting value of
i
is 5, and the loop counter will decrement from 5 to 0. The result of the above program is:
DATA[0] = -5
DATA[1] = -4
DATA[2] = -3
DATA[3] = -2
DATA[4] = -1
DATA[5] = 0
Since we have loops, someone might ask, "Can I skip a particular iteration within the loop?" The answer is yes. Suppose I don't want the fourth iteration.
Example:
Define a temp=i=int
Define an output=DATA=ARRAY[0..4] OF INT
FOR #i := 0 TO 4 DO
IF I = 3 THEN
CONTINUE;
END_IF;
#DATA[i] := i + 1;
END_FOR;
The
CONTINUE
keyword skips the current iteration. That is, when the loop counter i
equals 3, this iteration is skipped. Therefore, the result of this program is:
DATA[0] := 1
DATA[1] := 2
DATA[2] := 3
DATA[3] := (previous value, unchanged)
DATA[4] := 5
Is there a keyword to exit the entire loop? Yes, there is. Suppose I have an array, and I need to compare the values in this array with an input number. Once I find the desired data, I need to know its position in the array.
Example:
Define an input=number=int
Define a temp=i=int
Define an output=site=int
Define an output=DATA=ARRAY[1..100] OF INT
(where DATA[1]=1
, DATA[2]=2
, ..., DATA[100]=100
)
FOR #i := 1 TO 100 DO IF DATA[i] = number THEN site := i; EXIT; END_IF; END_FOR;
We compare the values based on the input number
. If the comparison is unsuccessful, the site
output retains its previous value. If the comparison is successful, it outputs the position of the data. For example, if we input number
as 35, when it compares to 35, site
will output 35, indicating that the data is in the 35th position. Someone might ask why i
did not loop to 100. This is because we have a condition to check. When the value of data[i]
equals the input number
, we execute the EXIT
keyword, which exits the entire loop. Even if the loop encounters EXIT
after just one iteration, the remaining iterations will not continue.
This concludes our discussion on the for loop. Originally, I planned to discuss two other topics together, but after considering that they differ from the for loop, I decided to separate them.
Notes on Using the for Loop:
The loop counter should not iterate to a value that does not exist. For example,
for i := 0 TO 9 DO
with an array defined asDATA=ARRAY[0..8] OF INT
. In this case, during the ninth iteration, the PLC will not have this data, and it will report an error. The diagnostic buffer will show an FB(C)xx area length error. This is because you have iterated to data that does not exist in the PLC. Since the program compiler does not detect this error, you must avoid it yourself.The value following "BY" must be a non-zero number. Of course, if you write 0, the system will prompt you that a value of 0 will cause an infinite loop.