Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!samsung!think!mintaka!ogicse!decwrl!sgi!bron@bronze.wpd.sgi.com From: bron@bronze.wpd.sgi.com (Bron Campbell Nelson) Newsgroups: comp.sys.sgi Subject: Re: Fortran bug? Summary: Not a bug; illegal code. Message-ID: <53943@sgi.sgi.com> Date: 17 Mar 90 21:46:59 GMT References: <1990Mar17.021042.20480@helios.physics.utoronto.ca> Sender: bron@bronze.wpd.sgi.com Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 43 In article <1990Mar17.021042.20480@helios.physics.utoronto.ca>, sysjohn@physics.utoronto.ca (John Chee Wah) writes: > > Here is a piece of code (isn't mines) that I think should work. Loops and > goto ends with a single continue statement. The program will work if the > two loops have separate statement labels. > System is 4D/240 with 3.2.1. > ----- > write(6,'(''Enter integer 0 or less and program will loop: '',$)') > read(5,*)iup > c > do 1 i=-1,1 > if(i.gt.iup)goto 1 > do 1 j=-1,1 > write(6,'(''j='',i4)')j > 1 continue > c > stop > end This question comes up over and over in different ways. This is not really a bug in the Fortran compiler; the code as written is not standard conforming, and its exact meaning is ambiguous. I admit that I think the compiler ought to give an error message, but it does not (and neither do most other Fortran compilers). The problem is that statement 1 is considered to be a statement in the inner loop. When you branch to it, you have branched from an outer block into an inner block, which is not allowed (just as branching directly to the "write" statement is not allowed). The Fortran compiler gets confused about whether it should "continue" back at the j loop, or the i loop. In fact it goes to the inner (j) loop which has not been properly initialized, picks up garbage off the stack, and goes. This topic gets hashed out in comp.lang.fortran every year or so. Many other Fortran compilers have similar behavior, some do it differently, some give a compile error. Using separate labels for the loops is the right thing to do. -- Bron Campbell Nelson bron@sgi.com or possibly ..!ames!sgi!bron These statements are my own, not those of Silicon Graphics.