# 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>.

## Notice

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.

## Programing Example

Counting loops

N100 $FOR P1= 10, 100, 2 P1 is pre-assigned the value 10 at loop start.
The counting loop is passed until P1
exceeds the value 100; then P1 is
incremented by 2 at the end of every loop
pass.

N110 X SIN [P1 * 5] Within the counting loop, the NC blocks N110
to N130 are executed.

N120 Y COS [P1 * 5]

N130 ...

N150 $ENDFOR

## Programing Example

Negative step width:

N100 $FOR P1= 100, 10, -2 P1 is pre-assigned the value 100 at loop start.
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

to N130 are executed..

N120 Y COS [P1 * 5]

N130 ...

N150 $ENDFOR

Loops not executed:

N100 $FOR P1= 100, 10, 1 P1 is pre-assigned the value 100 at loop start.
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.

N120 Y COS [P1 * 5]

N130 ...

N150 $ENDFOR

Endless loop:

N100 P2=20

N110 $FOR P1= 100, 10, 0 Endless loop

N120 $IF P2 == 50

N130 $BREAK

N140 $ENDIF

N150 $ENDFOR