Fortran Fixing Goto Error in Fortran 90 Program

  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion revolves around issues with using the GOTO statement in Fortran, particularly the error encountered when trying to jump to a label associated with the "end program" directive. It is clarified that "end program" is not an executable statement but a compiler directive, which is why the label cannot be recognized. Suggestions include using a CONTINUE or RETURN statement instead of STOP to avoid program termination and to ensure proper flow control. Additionally, concerns are raised about jumping into blocks of code, which can lead to undefined behavior and compiler warnings. The conversation emphasizes that GOTO statements are generally discouraged in Fortran due to their potential to create complex, unmanageable code structures. A separate request for help with printing multiple arrays as a table is noted, with advice to start a new thread for that topic.
fluidistic
Gold Member
Messages
3,928
Reaction score
272
I don't understand why I get an error when I use the goto "command" like this:


(...)

goto 11

(...)

11 end program


----------------------------------------------------------
The (...) represent the other part of my program. I tried to change the place of the 11. Because if I compile my program with the "11 end program" line I get the error "Label 11 referenced at (1) is never defined" so basically it doesn't see the 11 in front of "end program".
When I changed the place of the "11" the program could compile.
Can someone explain me why fortran can't see the 11 in front of the end program?
Now I need to think about a substitute of my original idea.:smile:
 
Technology news on Phys.org
Try attaching the statement number to a "continue" statement right before the "end program" statement. I suspect the problem is that "end program" is not an executable statement, but simply a signal (directive) to the compiler. You might as well try to put a statement number on a "dimension" statement.
 
End is not an executable statement it's a compiler directive. Use this:

11 STOP
END
 
I would prefer
11 CONTINUE
or
11 RETURN
rather than
11 STOP

If you use a "stop" statement, you may get some output saying "program terminated by stop statement at line xxx in subprogram main" or something similar.

Even better, replace the
GOTO 11
with
RETURN
 
Thanks guys. My program now compile and works well (I believe. I must test an .and. command but I think it works great).
Instead of stopping the program as I initially thought, I redirect to the statement before a "if (...)
end if" so basically my program restart from a certain point.

So the main problem is that "end program" is not an executable statement... wow.
 
i have similar problem in go to statement. i get a warning, please answer my problem rapidly.
go to 3
.
.
.
.
.
3 write(10,*),"variables=x,y,say"
.
.
.
.
end program
 
post your warning rapidly
 
azar8 said:
please answer my problem rapidly.
We will answer your problem rapidly when we get around to it.
 
warning is:
Warning: A jump into a block from outside the block has occurred. [3]
 
  • #10
A "block" is something like a DO loop, or an IF ... THEN .. ELSE ... ENDIF structure.

If you write code like
Code:
GOTO 10
...
DO I = J, K
...
10 ...
...
ENDDO
It isn't very obvious what should happen at the ENDDO statement, because I, J and K might not have any values, or they might have values that mean you shouldn't be executing the code insude the loop at all.
Similarly for
Code:
IF (X .GT. 0}
...
10 ...
...
ELSE
...
ENDIF
you could junp to label 10 when X was not > 0.

An optimising compiler might generate code that just doesn't work at all in those situations. For example at statement 10 the compiler should be able to assume that X really is greater than 0, so dividing by X can't produce a "divide by zero" error when the program runs, or evaluating X**0.25 can't prodice an error saying "X is negative", etc.

That's why Fortran says it is illegal to jump into a block from outside - though your compuler let you off with a warning message instead of an error.
 
  • #11
GOTO's are a poor programming practice in Fortran. It is a desperate last resort which results in spaghetti code.
 
  • #12
Hey guys could anyone teell me a print format by which i could print 3 arrays as a table like first array on the first column and the second array on the second colomn,,, in short i just want to print 3 RRAYS TOGETHER as a table each array ON ONE COLUMNthanks in advance
 
  • #13
Hardy, you might want to create a new thread with a more clear definition on what you need help with. You just randomly posted in someone's thread, in case you didn't realize.
 
  • #14
hardy03 said:
Hey guys could anyone teell me a print format by which i could print 3 arrays as a table like first array on the first column and the second array on the second colomn,,, in short i just want to print 3 RRAYS TOGETHER as a table each array ON ONE COLUMN

Please start a new thread.
 

Similar threads

Replies
12
Views
2K
Replies
17
Views
6K
Replies
5
Views
5K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
16
Views
2K
Back
Top