I think the quoted section 22.214.171.124 above explains how the trip count is evaluated and incremented. If you want to write portable code, then you should keep your code consistent with that, even if some compilers allow you to violate those constraints.
Another problem with the original code is the value of the index after the loop. Any do loop of the form
do i = m1, m2, m3 where
m2=huge(i) cannot be standard conforming. Consider a simple example
do i = huge(i)-1, huge(i)
which should only execute twice. This is clearly not a problem with the trip count value. After loop execution, the vaue of
i should be
huge(i)+1, which cannot occur. As a practical matter, the loop may in fact execute the correct number of times, and if the value of
i is never referenced afterwards, then the violation may be benign. But in a technical sense, that loop violates the standard.
You give one possible solution to the problem by using do loop parameters with a different KIND. Another workaround is something like
i = -huge(i)
! loop code here
if ( i == huge(i) ) exit
i = i + 1
You might be able to do the same thing with a
do while loop, but it takes some care to stay within the range of the model integers. This likely will not to be as efficient as a do loop, but that is why do loops are defined with trip counts, to make them very efficient to execute in the first place.
Another related question comes to mind. Suppose one had an array expression
Is that array range expression required to work, or does it have the same kind of index range restrictions as the equivalent do loop?