Iteration Statements
Iteration statements specify that the group of associated statements is executed repeatedly. The FOR statement is used if the number of iterations can be determined in advance; otherwise, the WHILE or REPEAT constructs are used.
- FOR: the FOR statement indicates that a statement sequence is repeatedly executed, up to the END_FOR keyword, while a progression of values is assigned to the FOR loop control variable. The control variable, initial value, and final value are expressions of the same integer type (e.g., SINT, INT, or DINT) and cannot be altered by any of the repeated statements. The FOR statement increments the control variable up or down from an initial value to a final value in increments determined by the value of an expression; this value defaults to 1.The test for the termination condition is made at the beginning of each iteration, so that the statement sequence is not executed if the initial value exceeds the final value.
- WHILE: the WHILE statement causes the sequence of statements up to the END_WHILE keyword to be executed repeatedly until the associated Boolean expression is false. If the expression is initially false, then the group of statements is not executed at all.
- REPEAT: the REPEAT statement causes the sequence of statements up to the UNTIL keyword to be executed repeatedly (and at least once) until the associated Boolean condition is true.
- EXIT: the EXIT statement is used to terminate iterations before the termination condition is satisfied. When the EXIT statement is located within nested iterative constructs, exit is from the innermost loop in which the EXIT is located, that is, control passes to the next statement after the first loop terminator (END_FOR, END_WHILE, or END_REPEAT) following the EXIT statement.
- FOR:
- WHILE:
- REPEAT: