Basic instructions
8.8 Program control operations
S7-1200 Programmable controller
320 System Manual, V4.2, 09/2016, A5E02486680-AK
Table 8- 157 Elements of the FOR statement
FOR "control_variable" := "begin" TO "end"
[BY "increment"] DO
statement;
;
END_FOR;
A FOR statement is used to repeat a sequence of
statements as long as a control variable is within
the specified range of values. The definition of a
loop with FOR includes the specification of an
initial and an end value. Both values must be the
same type as the control variable.
You can nest FOR loops. The END_FOR state-
ment refers to the last executed FOR instruction.
Table 8- 158 Parameters
Required. An integer (Int or DInt) that serves as a loop counter
Required. Simple expression that specifies the initial value of the control variables
Required. Simple expression that determines the final value of the control variables
"increment" Optional. Amount by which a "control variable" is changed after each loop. The "increment"
has the same data type as "control variable". If the "increment" value is not specified, then
the value of the run tags will be increased by 1 after each loop. You cannot change "incre-
ment" during the execution of the FOR statement.
The FOR statement executes as follows:
● At the start of the loop, the control variable is set to the initial value (initial assignment)
and each time the loop iterates, it is incremented by the specified increment (positive
increment) or decremented (negative increment) until the final value is reached.
● Following each run through of the loop, the condition is checked (final value reached) to
establish whether or not it is satisfied. If the end condition is not satisfied, the sequence of
statements is executed again, otherwise the loop terminates and execution continues
with the statement immediately following the loop.
Rules for formulating FOR statements:
● The control variable may only be of the data type Int or DInt.
● You can omit the statement BY [increment]. If no increment is specified, it is automatically
assumed to be +1.
To end the loop regardless of the state of the "condition" expression, use the EXIT statement
(Page 324). The EXIT statement executes the statement immediately following the
END_FOR statement.
Use the CONTINUE statement (Page 323) to skip the subsequent statements of a FOR loop
and to continue the loop with the examination of whether the condition is met for termination.