FOR statement in ST
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 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. 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. |
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
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
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
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