Trail: PROIV Documentation > Developer > PROIV Developer > Developing Functions > Events and Logic > FOR_TO_ENDFOR
|
|
Purpose
|
FOR TO ENDFOR controls execution of a loop. A loop is a construct containing a statement or block of statements that is executed repeatedly until a certain condition is met.
|
Syntax
|
FOR control-variable = initial-value TO final-value {BY increment/decrement} ENDFOR
or
FOR control-variable = initial-value DOWNTO final-value {BY increment/decrement} statement or block
ENDFOR
|
Operation
|
control-variable must be a numeric variable.
initial-value is any numeric literal or expression that represents the start value of the loop counter. This is computed only once, upon entry to the FOR loop and control-variable is initialized to this value.
final-value is any numeric literal or expression that represents the end value of the loop counter. This is computed before each iteration of the loop.
increment/decrement is any numeric literal or expression representing the number by which initial-value is to be incremented/decremented at each iteration of the loop. The default is 1.
|
Remarks
|
If a statement in the loop changes the value of increment, decrement, final-value, or control-variable, the number of times the loop is executed is affected. The statements within the loop are executed repeatedly, while control-variable is incremented or decremented from initial-value to final-value.
In the TO form, final-value must be greater than or equal to initial-value to cause the loop to execute.
In the DOWNTO form, final-value must be less than or equal to initial-value.
In the normal execution of a FOR loop, control-variable is initialized to initial-value. Control-variable is then compared to final-value, and if it is greater than (using the TO form) or less than (using the DOWNTO form) final-value, control is passed to the statement following the ENDFOR statement. Otherwise, control is passed to the statement or block within the FOR loop. When control reaches the ENDFOR statement, control-variable is incremented or decremented and then compared with final-value. Depending on the result of the comparison, the statement or block within the loop is executed again or the loop terminates.
|
Example
|
FOR #I = 1 TO 10
FAC = FAC + #I
ENDFOR
The statement FAC = FAC + #I is executed 10 times. The first time through the loop, the variable #I has the value of 1, the second time through it has the value 2, and so on. For each value of #I from 1 to 10, the value of #I is added to FAC.
|
Comment on this topic
Topic ID: 520059