View previous topic :: View next topic |
Author |
Message |
technophobe
Joined: 28 May 2007 Posts: 29
|
Posted: Wed Mar 12, 2008 5:18 pm Post subject: Strange Loop Feature |
|
|
Hi Guys,
I've been debugging a code now for several hours and have noticed something wierd (turns it out was the source of my error).
I have been using fortran on and off for several years now, I have a lot of experience with Matlab and thought that I understood programming structure pretty well until today. I was surprised by the output of the following few lines of code:
Code: | program testing_loop
!-----------------
INTEGER :: I
!-----------------
DO I=1,10
WRITE(*,*) I
ENDDO
WRITE(*,*) 'Loop has ended'
WRITE(*,*) 'I=', I
end program testing_loop |
The output, I was surprised to find, was the numbers 1 through 10 followed by: 'Loop has ended I=11'.
I thought that the loop, having terminated when I=10, would produce the output 'I=10' but for some reasons it has incremented the I counter to 11. The same piece of code in matlab, for example, would give an output of 10 and this is what I expected here.
Is this normal behaviour for fortran? I wasn't aware of this feature until today, I've spent several hours debugging my code and it turns out that it had something to do with this; essentially I had assumed that after the loop the value of 'I' would be 10 and based my next set of calculations on this fact.
I'm happy to have found the bug but wondered if this is normal behaviour for FTN95? |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7926 Location: Salford, UK
|
Posted: Wed Mar 12, 2008 6:05 pm Post subject: |
|
|
I think that you will find that there is no normal behaviour in this situation.
The Fortran standard will say that the value of the DO index, on normal termination of the loop, is not defined (it can be anything).
You can store the index elsewhere (put k=i in the loop) and use the stored value on termination. |
|
Back to top |
|
|
technophobe
Joined: 28 May 2007 Posts: 29
|
Posted: Thu Mar 13, 2008 12:00 am Post subject: |
|
|
Thanks Paul,
I never knew that the DO index wasn't defined. I am perhaps too used to using Matlab where the FOR index is retained between loops.
Now that I know this I'll be more careful in future,
Cheers,
B |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Thu Mar 13, 2008 2:15 am Post subject: |
|
|
My understanding of this is that the fortran 9x standard does define the value of the index and in your case the value is 11. More generally :-
do i = 1,n
! code with cycle and exit options
end do
if (i <= n) then ! the loop terminated via an exit
if (i == n+1) then ! the loop ran to completion
This is useful when searching for a value in a list. "I" points to the found value or the next vacant entry beyond the defined list, such as:-
do i = 1,n
if (value == list_of_values(i)) exit
end do
if ( i > n) then ! new value
Regards John |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7926 Location: Salford, UK
|
Posted: Thu Mar 13, 2008 9:11 am Post subject: |
|
|
The Fortran standard states "When an active DO construct becomes inactive, the do-variable, if any, retains its last defined value".
To me this is ambiguous. It depends on how the iteration is coded by the compiler.
In practise I think that you will find that most Fortran compilers behave in the same way as FTN95 but for complete portability you should not depend on this. |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri Mar 14, 2008 12:00 am Post subject: |
|
|
Paul,
I remember years ago when fortran 90 first came out and Tom Lahey did a tour promoting the improvements in the F90 standard. One that I (may have mistakenly) taken from his talk was the improved definition of how DO loops operate, including how the limits are defined on entry to the loop and the loop exits when the counter fails the test, for example
"DO I = 1,I"
should be valid if "I" had a value on entry to the loop. I continues to increment until it exceeds the value "I" had before the loop started.
For do I = 1,n, if I<=n on exit then the loop terminated internally, wheras the loop completed normally if I>n.
I've been using this assumption for 15 years !
John |
|
Back to top |
|
|
weaverwb
Joined: 04 Aug 2005 Posts: 37 Location: Monterey
|
Posted: Sun Mar 16, 2008 8:48 am Post subject: |
|
|
Hi all,
I've been using Fortran since Fortran II so I've seen a lot of compilers come and go. Most have claimed that the index is not defined after leaving the loop but all tested for 'exceed'; i.e., in your case it would be 11. Personally, I've always taken their word for it and not trusted the value of the index after completion of the loop. |
|
Back to top |
|
|
|