Description

The FOR instruction is used when the number of occurrences can be determined in advance. Otherwise WHILE or REPEAT are used.

The FOR instruction repeats an instruction sequence until the END_FOR instruction. The number of occurrences is determined by start value, end value and control variable.

The control variable, initial value and end value must be of the same data type (INT, UINT, DINT or UDINT).

The control variable, initial value and end value can be changed by a repeated instruction. This is a supplement to IEC 61131-3.

The FOR instruction increments the control variable value of one start value to an end value. The increment value has the default value 1. If a different value is to be used, it is possible to specify an explicit increment value (variable or constant). The control variable value is checked before each new loop. If it is outside the start value and end value range, the loop will be left.

Before running the loop for the first time a check is made to determine whether incrementation of the control variables, starting from the initial value, is moving toward the end value. If this is not the case (e.g. initial value ≤ end value and negative increment), the loop will not be processed. The control variable value is not defined outside of the loop.

The DO instruction identifies the end of the repeat definition and the beginning of the instruction(s).

The occurrence may be terminated early using the EXIT. The END_FOR instruction marks the end of the instruction(s).

Example: FOR with Increment 1

FOR with increment 1

FOR with Increment not Equal to 1

If an increment other than 1 is to be used, it can be defined by BY. The increment, the initial value, the end value and the control variable must be of the same data type (DINT or INT). The criterion for the processing direction (forwards, backwards) is the sign of the BY expression. If this expression is positive, the loop will run forward; if it is negative, the loop will run backward.

Example: Counting forward in Two Steps

Counting forward in two steps

Example: Counting Backwards

Counting backwards

FOR i:= 10 TO 1 BY -1 DO (* BY < 0 : Backwards.loop *)
  C:= C * COS(B) ; (* Instruction is executed 10 x *)
END_FOR ;

Example: "Unique" Loops

The loops in the example are run exactly once, as the initial value = end value. In this context it does not matter whether the increment is positive or negative.

FOR i:= 10 TO 10 DO (* Unique Loop *)
  C:= C * COS(B) ;
END_FOR ;

or

FOR i:= 10 TO 10 BY -1 DO (* Unique Loop *)
  C:= C * COS(B) ;
END_FOR ;

Example: Critical Loops

If the increment is j > 0 in the example, the instruction is executed.

If j < is 0, the instructions are not executed because the situation initial value < only allows the end value to be incremented by ≥ 0.

If j = 0, the instructions are executed and an endless loop is created as the end value will never be reached with an increment of 0.

FOR i:= 1 TO 10 BY j DO
  C:= C * COS(B) ; 
END_FOR ;