Fortran 90 and later strongly recommend not to use goto
statement.
However, I still feel forced to use it in either of the two cases:
Case 1 -- Instruct to re-enter the input value, e.g.
program reenter
10 print*,'Enter a positive number'
read*, n
if (n < 0) then
print*,'The number is negative!'
goto 10
end if
print*,'Root of the given number',sqrt(float(n))
stop
end program reenter
Case 2 -- To comment a large continuous part of a program (an equivalent to /* ... */
in C).
Eg.
print*,'This is to printed'
goto 50
print*,'Blah'
print*,'Blah Blah'
print*,'Blah Blah Blah'
50 continue
print*,'Blahs not printed'
How can I get rid of using goto
statement and use some alternatives in the above two cases in Fortran 90?
Case 1
What you have is an indefinite loop, looping until a condition is met.
Or one could use a
do while
lump.Case 2
A non-Fortran answer is: use your editor/IDE's block comment tool to do this.
In Fortran, such flow control can be
or (which isn't Fortran 90)
But that isn't to say that all
goto
s should be avoided, even when many/all can be.depending on what you mean by "continuous part of a program" case 2 could be jumping out of some block structure, eg:
If you encounter such thing it can be quite a challenge to unravel the code logic and replace with modern structured coding. All the more reason not to "comment" that way..