ST instruction FOR

The FOR loop is used to execute instructions with a specific number of retries.

Syntax:

FOR <counter> := <start value> TO <end value> {BY <increment> } DO
    <instructions>
END_FOR;

The section inside the curly brackets {} is optional.

TwinCAT executes the <instructions> until the <counter> is not greater, or - if the step size increment is negative - is less than the <end value>. This is checked before the <instructions> are executed.

Whenever the <instructions> have been executed, the <counter> is automatically incremented by the step size <increment>. The step size <increment> can have any integer value. If you do not specify a step size, the default step size 1 is used.

Example:

FOR nCounter := 1 TO 5 BY 1 DO
    nVar1 := nVar1*2;
END_FOR;
nErg := nVar1; 

If you have set nVar1 to 1, nVar1 has the value 32 after the FOR loop.

ST instruction FOR 1:

End value of the FOR loop

The <end value> must not have the same value as the upper bound of the data type of the counter.

In addition to the IEC 61131-3 standard, you can use the CONTINUE instruction within the FOR loop.

See also: