9 for loop, For loop, Drive plc developer studio – Lenze DDS v2.3 User Manual

Page 46

Advertising
background image

Drive PLC Developer Studio

Programming languages

4-8

l

DDS EN 2.3

4.3.9

FOR loop

Use the FOR loop to program repetitive procedures.

Syntax:

INT_Var :INT;

FOR

<INT_Var>:=<INIT_VALUE> TO <END_VALUE> {BY <step size>} DO

<instructions>

END_FOR

;

The part in curly brackets {} is optional.

The <instructions> are executed as long as the counter<INT_Var> is not greater than
the<END_VALUE>.

A check is performed before the <instructions> are executed so that the
<instructions>

will never be executed if <INIT_VALUE> is greater than <END_VALUE>.

Whenever the section <instructions> has been executed, <INT_Var> will be increased
by <step size>.

The step size can have any integer value. If no other step size is specified, step size 1 will be
used. The loop must terminate because <INT_Var> will only become greater.

If <INIT_VALUE> is greater than <END_VALUE> and <step size> is negative, the FOR
loop is counted in opposite direction.

If <INIT_VALUE> is less than <END_VALUE> and <step size> is negative, the FOR loop
will not be executed.

Example:

FOR

Counter:=1 TO 5 BY 1 DO

Var1:=Var1*2;

END_FOR

;

Res:=Var1;

Assuming that the variable Var1 has been pre-assigned value 1, it will be 32 after the FOR loop.

Caution!

<END_VALUE>

must not be the limit value of the counter <INT_VAR>.

If, for instance, the variable Counter is of type SINT, <END_VALUE> must not be 127 since
otherwise the loop would be endless.

After completion of the FOR loop, Counter has a value 6.

Show/Hide Bookmarks

Advertising