Conditionals supported and specified by IEC-61131-3
|
NOTE!
Check if equipment supports conditionals!
|
IF
The statement makes it possible for a group of statements to be executed only if the boolean expression associated with it is true. If the condition is false, only the group of statements after the ELSE keyword will be executed, if it is implemented.
Description |
Example |
IF ... THEN ...
ELSIF ... THEN ...
ELSE ... END_IF |
D := B * B - 4.0 * A * C; IF D < 0.0 THEN N:= 0; ELSIF D = 0.0 THEN N:=1; X1:= - B/(2.0+A); ELSE N:=2; X1:=(- B + FB_SQRT(D))/(2.0*A); X2:=(- B - FB_SQRT(D))/(2.0*A); END_IF; |
CASE
The instruction consists of a selector and groups of instructions. Each group of statements is labeled by one or more literals, enumerated values, or subranges. The first case that meets the value of the selector will have its instruction group executed. If the selector value does not occur, the sequence of commands after the ELSE keyword must be executed. Otherwise none of the instruction sequences should be executed.
The data types of these labels must match the data type of the variable specified in the selector.
Description |
Example |
CASE ... OF ... ELSE ... END_CASE |
C:=0; CASE A OF 1: B:= 10; 2: B:= 20; 3,5 B:= 30; 8..10 B:= 40; ELSE B:= 50; C:=1; END_CASE |
FOR
The statement makes it possible for a sequence of statements to be executed repeatedly, while a progression of values is assigned to its control variable. The FOR statement increments or decrements the control variable from an initial value to a final value determined by the value of an expression. If the BY construct is omitted, the increment value defaults to 1. The start value and end value of the control variable must be expressions of the same integer type (for example, SINT, INT, or DINT).
Description |
Example |
FOR ... TO ... BY ... DO ... END_FOR |
FOR I:=1 TO 100 BY 2 DO IF A[I] > 50 THEN B := 1; ELSE B := 0; END_IF END_IF END_FOR |
WHILE
In the statement, the sequence of statements grouped up to the END_WHILE keyword are executed repeatedly until the associated Boolean expression is false. If initially the expression is false, the group of statements will not be executed.
Description |
Example |
WHILE ... DO ... END_WHILE |
J:=0; WHILE J <= 100 & A[J] > 50 DO B:= B+2; END_WHILE |
REPEAT
The statement causes the sequence of statements wrapped up to the UNTIL keyword to be executed repeatedly at least once, until the Boolean condition associated with it is true.
Description |
Example |
REPEAT ... UNTIL ... END_REPEAT |
J:=0; REPEAT J:= J+2; UNTIL J > 100 OR A[J] > 50; END_REPEAT |
CONTINUE
The statement must be used to skip the remaining statements of the iteration loop, located after the CONTINUE keyword until the loop terminator (END FOR, END WHILE or END REPEAT).
Description |
Example |
CONTINUE |
J:=1; WHILE (J <= 100 AND A[J] <= 50) DO IF (J MOD 3 = 0) THEN CONTINUE; END_IF; J:=J+1; END_WHILE; |
EXIT
The statement must be used to terminate iterations before the termination condition is satisfied.
When the EXIT statement is located in nested iterations, the exit must be from the innermost loop in which the EXIT keyword is located, that is, control must pass to the next statement after the first loop terminator (END FOR, END WHILE or END REPEA 'I') after the OUTPUT declaration.
Description |
Example |
EXIT |
FOR I:=1 TO 100 BY 2 DO IF A[I] > 50 THEN B := 1; EXIT; END_IF END_IF END_FOR |