Fixing Goto Error in Fortran 90 Program

  • Context: Fortran 
  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around issues related to the use of the GOTO statement in Fortran 90 programs, specifically addressing errors encountered when using GOTO to jump to labels near the end of a program. Participants explore the nature of the "end program" directive and suggest alternatives to improve code structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion about an error related to the GOTO statement and the "end program" directive, noting that the label is not recognized by the compiler.
  • Another suggests using a CONTINUE or RETURN statement instead of STOP at the end of the program, emphasizing that END is a compiler directive and not an executable statement.
  • A participant mentions redirecting the program flow to a point before an IF statement instead of stopping the program, indicating a successful resolution of their issue.
  • Another participant shares a similar issue with a GOTO statement, receiving a warning about jumping into a block from outside, prompting a discussion about the implications of such jumps in structured programming.
  • One participant argues that GOTO statements are poor programming practice in Fortran, suggesting they lead to disorganized code.
  • A separate thread of discussion emerges with a participant asking for help with printing arrays in a tabular format, which is deemed off-topic by others.

Areas of Agreement / Disagreement

Participants generally agree that the "end program" directive is not executable and that using GOTO statements can lead to problematic code structures. However, there is no consensus on the best practices for using GOTO, as some defend its use in certain contexts while others criticize it.

Contextual Notes

Some limitations in the discussion include the lack of clarity on specific programming contexts and the potential for undefined behavior when jumping into blocks from outside. The discussion also reflects varying levels of understanding of Fortran's control flow mechanisms.

Who May Find This Useful

Programmers working with Fortran, particularly those dealing with control flow and program structure, may find this discussion relevant. It may also be useful for those learning about best practices in coding and error handling in programming.

fluidistic
Gold Member
Messages
3,932
Reaction score
283
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 ·
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K