Re: use 'go to' doing some case studies and induced problems



In article <1153362148.122922.208210@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Mike <acout@xxxxxxx> wrote:
Hi,

Let's say there will be some case studies after the main program.
Usually, I do this:

program main
go to 100 !case 1
go to 200 !case 2
go to 300 !sensitivity study..
....
100 continue
case 1......
stop ' case 1'
.....


In what follows I am following Mike's example of avoiding
subroutines, though that may well not be the best way to go.
I suspect from his description he would comment out two of those
go to statements. If so, how about

program main
integer which
print *,'Enter 1,2, or 3 for case 1, case 2, sensitivity study'
read *, which
go to (100,200,300) which
...

if using f66 or old-fashioned f77? (Metcalf and Reid "Effective Fortran
77" (1990) p160, which predated f90, already recommended replacing a
computed GO TO by a block IF. Of course the CASE construct is even
better but wasn't in f77.)

In f95 that code would still work but give an obsolescence warning.
One way to avoid that is to replace the last line by

select case(which)
case(1)
go to 100
case(2)
go to 200
case(3)
go to 300
case default
print *,'If you know what that means, implement it!'
stop
end select

which also deals with the possibility of the user entering a number
other than 1,2 or 3.

I would also replace "go to 100" by whatever Mike had between
"100 continue" and "stop 'case 1'" if brief, but it would be hard
to read the entire case construct if it was, say, 500 lines. If it
is, then subroutines really do help a lot. My own guide is that if
a program unit is too long for one screen it ought to be split up,
unless it's a module or main program in which what's before CONTAINS
is short enough, and so is each subprogram between CONTAINS and
either END PROGRAM or END MODULE. I just hope nobody with access to
my disk space is reading this and finding counter-examples :-)

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@xxxxxxxxx phone (+64)(4)463 5341 fax (+64)(4)463 5045
.



Relevant Pages