Path: utzoo!attcan!uunet!mcvax!dik From: dik@cwi.nl (Dik T. Winter) Newsgroups: comp.lang.fortran Subject: Re: Dubious Fortran Construct Keywords: Standard Fortran 77 Message-ID: <7770@boring.cwi.nl> Date: 13 Dec 88 21:57:18 GMT References: <6172@eagle.ukc.ac.uk> Organization: CWI, Amsterdam Lines: 44 In article <6172@eagle.ukc.ac.uk> trh@ukc.ac.uk (T.R.Hopkins) writes: > > ++ do 10 i = 1,n > ++ if (a(i) .eq. 0) goto 10 > ++ do 10 j = 1,n > ++ b(i,j) = 1/a(i) > ++10 continue > + > +This is illegal according to the ANSI standard. The range of a DO loop, by > > Section 11.10.2 states > > A DO-loop is either active or inactive. Initially inactive, a DO-loop > becomes active only when its DO statement is executed. > > > Seems to me that when the GOTO is executed the inner loop is inactive. > The effect of the transfer is thus to increment the outer loop counter > (see 11.10.3 and 11.10.4) and restart the outer iteration. Wrong conclusion. As far as I understand this is illegal in Fortran 77 and was in Fortran 66. So the question is, why do many compilers accept it? That is simple: in Fortran 66 it was legal to jump into a loop if you had jumped out of it first: do 10 i = 1, n junk goto 100 10 continue more junk 100 still more junk c if we get here through normal flow, the jump should not be taken. goto 10 was completely legal (it was called an extended do loop range). But you were only allowed to jump to label 10 if the loop was active (as the quote above defines). Many compilers maintained the extended range out of compatibility reasons (I understand Fortran 77 dropped it), and as it is very difficult to check whether the loop is active indeed they allow all kinds of jumps into loops. But if the loop was inactive the loop control variable is undefined and so you get unpredictable behaviour. -- dik t. winter, cwi, amsterdam, nederland INTERNET : dik@cwi.nl BITNET/EARN: dik@mcvax