Loops supported and specified by IEC-61131-3
|
NOTE!
Check if equipment supports conditionals!
|
Loop - FOR
The FOR statement enables a sequence of instructions to be executed repeatedly while a progression of values is assigned to its control variable. The FOR statement increments or decrements the control variable from a starting value to an ending value determined by the value of an expression. If the BY construct is omitted, the increment value defaults to 1. Both the starting and ending values must be expressions of the same integer type (e.g., USINT, INT, WORD, or DINT).
FOR <counter> := <initial value> TO <end value> {BY <increment>} DO
<instructions>
END_FOR
|
|---|
The section inside the curly braces {} is optional
WPS executes <instructions> until <counter> reaches the <end value>. For positive increments, execution continues until <counter> exceeds the <end value>. For negative increments, execution continues until <counter> falls below the <end value>. This condition is checked before executing <instructions>.
Example
The example below shows one way to use the FOR loop to find the largest value in a global array of type USINT.
VAR addr : WORD; currentValue : USINT; maxValue : USINT; arr : ARRAY[0..5] OF USINT := [17, 9, 30, 25, 59, 41]; END_VAR
currentValue := 0; maxValue := 0;
FOR addr := 0 TO 10 BY 1 DO currentValue := arr[addr]; IF currentValue > maxValue THEN maxValue := currentValue; END_IF END_FOR |
|---|
At the end of the execution of the example code, the value of maxValue should be 59.
Loop - WHILE
In a WHILE statement, the sequence of statements enclosed up to the END_WHILE keyword is repeatedly executed as long as <boolean expression> evaluates to true. If <boolean expression> is false from the start, the statements are not executed at all.
WHILE <boolean expression> DO
<instructions>
END_WHILE
|
|---|
Example
The example below demonstrates a WHILE loop that executes ten times.
VAR counter : USINT; END_VAR
counter := 0;
WHILE counter < 100 DO counter := counter + 10; END_WHILE |
|---|
At the end of the execution of the example code, the value of counter should be 100.
Loop - REPEAT
In the REPEAT statement, the sequence of instructions (<instructions>) grouped up to the UNTIL keyword is executed repeatedly at least once, until the boolean condition associated with it (<boolean expression>) is true.
REPEAT
<instructions>
UNTIL <boolean expression> END_REPEAT
|
|---|
Example
The example below illustrates the use of a REPEAT loop to find the smallest multiple of 13 that is diferent than 13.
VAR number : INT; module : INT; result : INT; found : BOOL; END_VAR
number := 1; result := 1; found := FALSE;
REPEAT module := number MOD 13; IF (module = 0) and (module <> 13) THEN result := number; found := TRUE; END_IF number := number + 1; UNTIL found END_REPEAT
|
|---|
At the end of the execution of the example code, the result value should be 26.
Statement - CONTINUE
The CONTINUE statement skips the remaining instructions in the current iteration of a loop, immediately proceeding to the next iteration. It applies from the CONTINUE keyword until the loop's termination keyword (END_FOR, END_WHILE, or END_REPEAT).
The example below shows one way to use the CONTINUE statement to find the sum of even numbers from 0 to 9.
VAR number : INT; module : INT; result : INT; END_VAR
number := 0; module := 0; result := 0;
WHILE number < 10 DO module := number MOD 2; IF module <> 0 THEN number := number + 1; CONTINUE END_IF result := result + number; number := number + 1; END_WHILE |
|---|
At the end of the execution of the example code, the value of result should be 20.
Statement - EXIT
The EXIT statement is used in a FOR, WHILE, or REPEAT loop to terminate the loop regardless of other termination conditions.
When the EXIT statement is used within nested loops, it causes an exit from the innermost loop where the EXIT keyword appears. Control is then transferred to the statement following the nearest loop terminator (such as END_FOR, END_WHILE, or END_REPEAT) after the EXIT statement.
The example below demonstrates how to use the EXIT statement to exit the FOR loop when you find the array index that matches the desired number.
VAR idx : WORD; currentValue : USINT; targetValue : USINT; found : BOOL; arr : ARRAY[0..5] OF USINT := [17, 9, 30, 25, 59, 41]; END_VAR
currentValue := 0; targetValue := 25; found := FALSE;
FOR idx := 0 TO 10 BY 1 DO currentValue := arr[idx]; IF currentValue = targetValue THEN found := TRUE; EXIT END_IF END_FOR |
|---|
At the end of the code execution, the value of idx should be 3, indicating the index where the number 25 is located in the array.