Counting loop ($FOR)
Counting loops permit the processing of statements n times. The number of loop passages is checked by a counting variable.
Syntax: |
The syntax of a counting loop starts with:
$FOR P..= <expr1> , <expr2> , <expr3> |
and always ends with:
$ENDFOR |
Here, P.. is the counting variable. Its start value is specified by <expr1>, its end value by <expr2> and the counting increment by <expr3>.
![]() | Only integer values may be used as counting variables. If decimal numbers are used, it is not possible to precisely represent the increment exactly (exception: powers of two) since a rounding error accumulates when added. This may lead to a loop that passes through one loop too few. |
Instead of the P parameters, it is also possible to use variables ("V.”) with write access.
If the counting increment is negative, the loop is aborted if the end value is undershot; if the counting increment is positive, the loop is aborted if the end value is exceeded. Programming the counting increment 0 leads to an endless loop and to the output of a warning.
Programming Example
Counting loops
The counting loop is passed until P1
exceeds the value 100; then P1 is
incremented by 2 at the end of every loop
pass.
to N130 are executed.
Programming Example
The counting loop is passed until
N110 X SIN [P1 * 5] P1 undershoots the value 10; then P1 is
decremented by 2 at the end of every loop
pass. In the counting loopsNC blocks N110
The counting loop is passed until
P1 exceeds the value 10.
N110 X SIN [P1 * 5] But here no loop since P1 is pre-assigned the value 100.