FOR statement in ST

Syntax
FOR ... TO ... BY ... DO
...
END_FOR;

Meaning

Use the FOR statement to specify that a group of statements (entered after DO up to END_FOR) is to be executed repeatedly, while a progression of values is assigned to the control variable of the FOR statement (entered after FOR). The FOR statement increments the control variable up or down from the initial value (assigned to the control variable) to the final value (entered after TO) in increments determined by the value of an expression (entered after BY). If the BY construct is omitted, the increment value defaults to 1.

The iteration of the FOR statement is terminated when the value of the control variable is outside the range specified by the TO construct.
The termination condition is checked at the beginning of each increment resp. decrement iteration: The group of statements will not be executed, if the value of the control variable has exceeded the final value (in case of a positive increment value) resp. if the value of the control variable has fallen below the final value (in case of a negative increment value).

The value of the control variable after completion of the FOR statement corresponds to the last value of the last iteration (hence: to the final value). If the group of statements is not executed at all, the value of the control variable is not changed.

The control variable, initial value and final value must be expressions of the same integer type ANY_INT (e.g. INT, SINT or DINT) and must not be altered by any of the repeated statements.
In contrast to that, it is possible to use a different integer type for the increment value. This makes it possible to decrement an initial value while using a control variable of an ANY_UNSIGNED datatype (see example 2).

No changes for control variable, initial value, final value and increment value within the repeated statements

Observe that logi.CAD 3 determines the number of the increment iterations before the FOR statement is started. Hence, do not enter any statements after DO up to END_FOR that change the expressions for the control variable, initial value, final value and/or increment value.

Indeterminable number of the increment iterations

If the number of the increment iterations is indeterminable (see example 4 because it is impossible to reach the final value in this example), the FOR statement is not started at all.

Example 1: FOR statement will be executed.
FUNCTION_BLOCK ExampleExecutedFORDocumentation
VAR
count, sum, I : INT;
END_VAR
count := 1;
sum := 0;
FOR I := 1 TO 50 BY 2 DO
(* "I" is the control variable, "1" is the initial value, "49" is the final value (although "50" has been entered), "2" is the increment value. *)
sum := ADD(sum, count);
count := ADD(count, 1);
END_FOR;
END_FUNCTION_BLOCK
Example 2: FOR statement will be executed.
FUNCTION_BLOCK ExampleExecutedFORDocumentation2
VAR
I : UINT;
END_VAR
FOR I := 100 TO 1 BY -1 DO
(* "I" is the control variable, "100" is the initial value, "1" is the final value, "-1" is the decrement value. *)
(* ... *)
END_FOR;
END_FUNCTION_BLOCK

Example 3: FOR statement will not be executed.
FUNCTION_BLOCK ExampleNotExecutedFORDocumentation
VAR
count, sum, I : INT;
END_VAR
count := 1;
sum := 0;
FOR I := 1 TO 10 BY -1 DO
(* It is not possible to increment initial value "1" by value "-1" (= decrement by "1") until final value "10" is reached. *)
sum := ADD(sum, count);
count := ADD(count, 1);
END_FOR;
END_FUNCTION_BLOCK
Example 4: FOR statement will not be executed.
FUNCTION_BLOCK ExampleNotExecutedFORDocumentation2
VAR
count, sum, I : INT;
byVar : INT;
END_VAR
count := 1;
sum := 0;
byVar := 0;
FOR I := 1 TO 10 BY byVar DO
(* The FOR statement is not executed because the number of the increment iterations is indeterminable. *)
(* ... *)
END_FOR;
END_FUNCTION_BLOCK