Counting loops

The counting loops enable the processing of statements n-times. The number of the loop passage is checked through a counting variable.

The Syntax of the counting loop begins with:

$FOR P <expr> = <expr1>, <expr2> <expr3> 

and always ends with:

$ENDFOR 

Here, P<expr> is the counting variable, its starting value is specified through <expr1>, its end value through <expr2> and the counting increment through <expr3>.

Counting loops 1:

Only integer values may be used as counting variables.

If using decimal numbers, it is generally not possible to precisely represent the increment (exception: powers of two) since a rounding error adds up when adding. This may lead to running through one loop too few.

In place of the P-Parameter also variables ("V") with write access or, if configured [6]-6.44, R-Parameters (see Chapter 9 ) could be used.

If the counting increment is negative, the loop is aborted if the end value is underflown, if the counting increment is positive the abortion of the loop takes place if the end value is exceeded. The programming of the counting increment 0 leads to an endless loop and to the output of a warning.

Programming example

N100 $FOR P1= 10, 100, 2 P1 is pre-allocated with 10 at loop beginning.
The counting loop is traversed till P1 has
exceeded the value 100, whereas 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

Programming example

Negative step width:

N100 $FOR P1= 100, 10, -2 P1 is pre-allocated with 100 at the loop
beginning. The counting loop is traversed till
N110 X SIN [P1 * 5] P1 has fallen below the value 10, whereas P1 is
decremented by 2 at the end of every loop pass.
Within the counting loops the NC blocks N110 to
N130 are executed.
N120 Y COS [P1 * 5]
N130 ...
N150 $ENDFOR
N100 $FOR P1= 100, 10, 1 P1 is pre-allocated with 100 at the loop
beginning. The counting loop is traversed till
P1 has exceeded the value 10, i.e. only once.
N110 X SIN [P1 * 5] Within the counting loops the NC blocks N110 to
N130 are executed.
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