There now follows a few examples of different loops,
loopy: DO i = 1, 30, 2 ... ! 15 iterations END DO loopy
According to the rules (given earlier) the fact that the upper bound is not exact is not relevant. The iteration count is INT(29/2) = 14, so i will take the values 1,3,..,27,29 and finally 31 although the loop is not executed when i holds this value, this is its final value.
DO j = 30, 1, -2 ... ! 15 iterations END DO
similar to above except the loop runs the other way (high to low). j will begin with the value 30 and will end up being equal to 0.
DO k = 30, 1, 2 ... ! 0 iterations ... ! loop skipped END DO
This is a false example in the sense that the loop bounds are literals and there would be no point in coding a loop of this fashion as it would never ever be executed! The execution is as follows, firstly, k is set to 30 and then the iteration count would be evaluated and set to 0. This would mean that the loop were skipped, the only consequence of its existence being that k holds the value 30.
DO l = 1,30 ... ! i = 1,2,3,...,30 ... ! 30 iterations END DO
As the stride is missing it must take its default value which is 1 This loop runs from 1 to (30 so the implied stride means that the loop is executed 30 times.
DO l = 30,1 ... ! zero-trip END DO
As the stride is missing it must take its default value which is 1 This loop runs from high to low (30 to 1) so the implied stride means that the loop is not executed. The final value of l will be 30.
Return to corresponding overview page