Q about Do loops

Q about Do loops

I am wondering if it's SAFE to change the limits of a Do loop in the middle of

the loop. The compiler does NOT give warning messages under some instances,

from what I have seen.

Here is an example;

integer*4 x(5)/7,2,8,9,13/

i1=2;   i2=5

Do i=i1,i2

  if(x(i).gt.5 .or x(i-1).gt.5)then





In the above example, I am processing an array of numbers, and I want to add

more numbers to the array as a result. The fundamantal question is; Are the

loop limits set and unchangeable when going into the loop, or are those limits tested

during each pass thru it?

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

It's "safe", but it has no effect. The number of iterations to be made by the do loop is determined only once, at the start of execution of the do loop. If you want to have loop limits that dynamically vary during the execution of the loop, then consider using DO WHILE (...) along with statements to initialise the "loop variable" and to increment it at the end of each iteration.

(Changing the do loop variable (i) itself in a DO i = a, b style loop is not safe.)

(Aren't you setting yourself up for writing off the end of the bounds of the x array?)

Well, in this case, the array can be "open ended'
That is, the upper limits are much larger then the initial contents.
Or the array can be in a subroutine where the upper limit is x(*).

Actually, the compiler would FLAG it as an error if I tried to modify I, as it should.
But this is a little bit different.

The "safe" way to do this is put the NEW elemets in a buffer, then copy them AFTER
the DO loop is finished. But that's more "messy."

BTW, can someone address the issue of interaction between source code editing and the
debugger? I saw a new problem which never occurred before, I think it's a bug.

It's really just a nuisance that can be avoided with "stop debugging" before editing
any source code. So it's no biggy. It does not always occur.

It is important to note that the number of itterations of the do loop you provided "Do i=i1,i2" will be defined when the DO loop starts. If you change the value of i2 during the loop, this will not change the number of itterations. This was a significant change from Fortran 77 to Fortran 90.
If you increase the value of i2, then the loop will not continue. For some reason, what you want to do has been removed from the standard.
As Ian has noted you need to change the structure of your loop.
I sometimes write my loops as: "Do i=i1,huge(i)" and then monitor if i exceeds i2 for termination. ( old habits as WHILE might read better, although "i" needs to be managed )


There's been no change with standard here. The iteration count in Fortran 77 was determined once, before execution of the body of the loop as well. In Fortran 66 the behaviour was hypothetically as you describe, but that standard prohibited changing the "terminal parameter", so the iteration count was still effectively fixed.

Leave a Comment

Please sign in to add a comment. Not a member? Join today