Before the first round, the initial value, the final value and the step are computed only once and in that order. Then, on every round:
It checks whether the control has gone past the final value. The final value is included.
If not, the body runs.
The step is added to the control.
ProgramThe 7 times table
AlgorithmTablaFori<-1To5DoWrite"7 x ",i," = ",7*i;EndForEndAlgorithm
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
The program finishes normally.
11, 5 and the step 1 are computed. The control starts at 1.
2Every round writes a line and adds the step: 2, 3, 4…
3With the control at 5 the body still runs: the final value is included.
4The control reaches 6, which is past the final value. The loop ends.
Figure 6.5-1.The control walks the values from the initial one to the final one, both included. When it goes past the final value, the loop ends.
A line with the values 1 to 6. The control starts at 1 and moves one by one; 5, the final value, is still included; on reaching 6 the loop ends.
The direction depends only on the sign of the step. With a negative step it counts backwards. If the initial value is already past the final one, the body does not run: