forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strange Loop Feature

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
technophobe



Joined: 28 May 2007
Posts: 29

PostPosted: Wed Mar 12, 2008 5:18 pm    Post subject: Strange Loop Feature Reply with quote

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
View user's profile Send private message Send e-mail
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7927
Location: Salford, UK

PostPosted: Wed Mar 12, 2008 6:05 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
technophobe



Joined: 28 May 2007
Posts: 29

PostPosted: Thu Mar 13, 2008 12:00 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Thu Mar 13, 2008 2:15 am    Post subject: Reply with quote

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
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7927
Location: Salford, UK

PostPosted: Thu Mar 13, 2008 9:11 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Fri Mar 14, 2008 12:00 am    Post subject: Reply with quote

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
View user's profile Send private message
weaverwb



Joined: 04 Aug 2005
Posts: 37
Location: Monterey

PostPosted: Sun Mar 16, 2008 8:48 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group