For - GE PACSystems RX7i Cpu Programmer's Reference Manual

Hide thumbs Also See for PACSystems RX7i:
Table of Contents

Advertisement

8.2.6
FOR ... DO Statements
The FOR loop repeatedly executes a statement list contained within the FOR ... DO ... END_FOR
construct. It is useful when the number of iterations can be predicted in advance, for example to
initialize an array. The number of iterations is determined by the value of a control variable which is
incremented (or decremented) from an initial value to a final value by the FOR statement.
By default, each iteration of the FOR statement changes the value of the control variable by 1. The
optional BY keyword can be used to specify an increment or decrement of the control variable by
specifying a (non-zero) positive or negative integer or an expression that resolves to an integer.
FOR loops can be nested to a maximum of ten levels.
Format
FOR Control_Variable := Start_Value TO End_Value [BY Step_Value] DO
Statement list;
END_FOR;
Where:
Control_Variable
The control variable. Can be an INT, DINT or UINT variable or parameter.
Start_Value
The starting value of the control variable. Must be an expression, variable, or
constant of the same data type as Int_Variable.
End_Value
The ending value of the control variable. Must be an expression, variable, or
constant of the same data type as Int_Variable.
Step_Value
(Optional) The increment or decrement value for each iteration of the loop.
Must be an expression, variable, or constant of the same data type as
Int_Variable. If Step_Value is not specified, the control variable is incremented
by 1.
Statement list
Any list of Structured Text statements.
Operation
The values of Start_Value, End_Value and Step_Value are calculated at the beginning of the FOR loop.
On the first iteration, Control_Variable is set to Start_Value.
At the beginning of each iteration, the termination condition is tested. If it is satisfied, execution of
the loop is complete and the statements after the loop will proceed. If the termination condition is
not satisfied, the statements within the FOR...END_FOR construct are executed. At the end of each
iteration, the value of Control_Variable is incremented by Step_Value (or 1 if Step_Value is not
specified).
The termination condition of a FOR loop depends on the sign of the step value.
Step Value Termination Condition
> 0
Control_Variable > End_Value
< 0
Control Variable < End Value
0
None. A termination condition is never reached and the loop will repeat infinitely.
As with the other iterative statements (WHILE and REPEAT), loop execution can be prematurely
halted by an EXIT statement.
To avoid infinitely repeating or unpredictable loops, the following precautions are recommended:
Do not allow the statement list logic within the FOR loop to modify the control variable.
Do not use the control variable in logic outside the FOR loop.
GFK-2950C
Chapter 8. Structured Text (ST) Programming
February 2018
377

Advertisement

Table of Contents
loading

This manual is also suitable for:

Pacsystems rx3iPacsystems rsti-ep

Table of Contents