While loop, L-force | plc designer – Lenze PLC Designer PLC Designer (R3-1) User Manual

Page 766

Advertising
background image

L-force | PLC Designer

General Editors

764

DMS 4.1 EN 03/2011 TD29

Let us assume that the default setting for Var1 is "1". Then it will have the value 32
after the FOR loop.

Note: If <END_VALUE> is equal to the limit value of counter <INT_VAR>, for
example if Counter - used in the example shown above - is of type SINT and if
<END_VALUE> is 127, then you will get an endless loop. So <END_VALUE> must not
be equal to the limit value of the counter !

Extension to the IEC 61131-3 standard (ExST):
The CONTINUE instruction can be used within a FOR loop.

WHILE loop
The WHILE loop can be used like the FOR loop with the difference that the break-off
condition can be any Boolean expression. This means you indicate a condition which,
when it is fulfilled, the loop will be executed.
Syntax:
WHILE
<Boolean expression> DO
<Instructions>
END_WHILE;
The <Instructions> are repeatedly executed as long as the <Boolean_expression>
returns TRUE. If the <Boolean_expression> is already FALSE at the first evaluation, then
the <Instructions> are never executed. If <Boolean_expression> never assumes the
value FALSE, then the <Instructions> are repeated endlessly which causes a relative
time delay.

Note: The programmer must make sure that no endless loop is caused. He does this
by changing the condition in the instruction part of the loop, for example, by
counting up or down one counter.

Example:

WHILE Counter<>0 DO
Var1 := Var1*2;
Counter := Counter-1;
END_WHILE

The WHILE and REPEAT loops are, in a certain sense, more powerful than the FOR loop
since one doesn't need to know the number of cycles before executing the loop. In
some cases one will, therefore, only be able to work with these two loop types. If,
however, the number of the loop cycles is clear, then a FOR loop is preferable since it
allows no endless loops.

Extension to the IEC 61131-3 standard (ExST):
The CONTINUE instruction can be used within a WHILE loop.

Advertising