Fortran bug?

Bron Campbell Nelson bron at bronze.wpd.sgi.com
Sun Mar 18 07:46:59 AEST 1990


In article <1990Mar17.021042.20480 at helios.physics.utoronto.ca>, sysjohn at 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 at sgi.com  or possibly  ..!ames!sgi!bron
These statements are my own, not those of Silicon Graphics.



More information about the Comp.sys.sgi mailing list