| New Reply |
Goto statement, mostly regarding Fortran |
Share Thread | Thread Tools |
| Jun28-11, 12:40 PM | #1 |
|
|
Goto statement, mostly regarding Fortran
Hey guys,
I've read that Borek (a member here) was told that the goto statement was "bad" during the 80's. I myself did some research on the Internet about that and it seems that: 1)Fortran used to have the goto statement up till Fortran 90. 2)It has been eradicated since Fortran 95. 3)In wikipedia one reads And also, how would you modify this part of one of my codes: Code:
98 write(*,*)"In order to solve the linear system, do you want to use Jacobi(1)'s method or Gauss-Seidel(2)'s one?" read(*,*)p if (p==1) then call jacobi(n_max,n,tol) else if (p==2) then call gauss(n_max,n,tol) else write(*,*)"Please choose a valid option" goto 98 end if Thanks a bunch in advance! (I'm really eager to know!) |
| Jun28-11, 02:00 PM | #2 |
|
Mentor
|
My experience with Fortran ended with the extended version of Fortran 77 that was available on VAX and Prime computers, so I'll use C++ for my two goto-less versions. This one most resembles your version:
Code:
cout << "In order to solve […]? ";
do
{
cin >> p;
if (p == 1)
jacobi (n_max, n, tol);
else if (p == 2)
gauss (n_max, n, tol);
else
cout << "Please choose a valid option (1 or 2): ";
} while ( ! ((p == 1) || (p == 2)) )
Code:
// Find out which algorithm to use.
cout << "In order to solve […]? ";
cin >> p;
while ( ! ((p == 1) || (p == 2)) )
{
cout << "Please choose a valid option (1 or 2): ";
cin >> p;
}
// Perform the chosen algorithm. At this point we are guaranteed
// that p is either 1 or 2.
if (p == 1)
jacobi (n_max, n, tol);
else
gauss (n_max, n, tol);
|
| Jun28-11, 02:58 PM | #3 |
|
|
Goto statements in any language make for hard to read code and are an inefficient way to do things. Your example of code is very simple and easy to follow, so it does not fall into the above category, but for example,
Code:
do 110 i=1,2000
if(a .ge. b(i)) then
go to 110
endif
indx = i
go to 112
110 continue
indx = 2000
112 z = float(indx-2)*c + c*(a - b(indx-1))/(b(indx) - b(indx-1))
|
| Jun28-11, 03:12 PM | #4 |
|
Recognitions:
|
Goto statement, mostly regarding Fortran
We like to call it "spaghetti".
You can compare it to a rope that has become entangled. As you may know it takes a lot of skill and effort to discern how it is entangled. And it is a lot of work to untangle it.
|
| Jun28-11, 03:20 PM | #5 |
|
Mentor
|
"The simple GO TO label exists, but is usually avoided — in most cases, a more specific branching construct will accomplish the same logic with more clarity." |
| Jun28-11, 04:15 PM | #6 |
|
Recognitions:
|
A previous and long thread about goto's. In some situations, for some programmers, they're better than the alternative:
http://www.physicsforums.com/showthread.php?t=308782 |
| Jun28-11, 04:19 PM | #7 |
|
Recognitions:
|
It's hard to follow and can get pretty insane if you have multiple goto's all over the place. Anyone trying to debug tons of goto's will probably throw their computer out the window.
|
| Jun28-11, 04:22 PM | #8 |
|
Recognitions:
|
![]() (I think there are goto's in it! )
|
| Jun28-11, 04:48 PM | #9 |
|
|
One of the ideals of writing a program is that it should be self-documenting. A WHILE loop or a FOR loop are somewhat more descriptive than the equivalent construct built out of GOTO and IF-THEN statements.
Most commonly needed ways to control the flow of your program can be more concisely and clearly described via methods without invoking a GOTO statement. Many of the exceptions can be rearranged into a simpler program that doesn't use GOTO. A lot of the GOTO hate is unwarranted, however. It's like the pirates vs global warming thing -- once upon a time GOTO was more or less all that was available, and people frequently wrote spaghetti* code. So people blamed GOTO as actually being the cause, rather than the lack of options simply being correlated with programming attitudes of the time. And, of course, there is also some naivety of the form "if we forbid GOTO, then people can't write spaghetti code!" which motivates some more of the GOTO hate. Sometimes, GOTO hate is taken to an extreme, turning into hate for BREAK, CONTINUE, some uses of exception handling, or even a RETURN that appears someplace other than the very last line of a function. *: Spaghetti code is so named because it's not clear (or even impossible!) to tell what is happening in the program without paying very careful attention to the thread of execution as it winds through the program. Also, there is the subsequent problem that it is very difficult to modify the program. |
| Jun28-11, 05:01 PM | #10 |
|
Recognitions:
|
For myself, I agree with your point of view on goto and other forms.
However, having worked in projects where lots of different junior programmers come along, I found it is best to keep the programming style rules simple and predictable. |
| Jun28-11, 05:37 PM | #11 |
|
|
Thanks a lot to you all guys for the insights. Too much to answer for me!
First, sorry I hadn't searched in PF about goto. The mentioned thread that rcgldr posted seems very interesting. About jtbell: Too bad I don't know C++ at all. But looking carefully at your code I can understand some stuff. However I do not see where is the "goto" statement in your first code. To Mark44, thanks for the link. Actually what I read on Fortran page seems different? Or my English is too bad so that I misunderstand. What do you say: And swartizm, here is your code: Code:
do 110 i=1,2000
if(a .ge. b(i)) then
go to 110
endif
indx = i
go to 112
110 continue
indx = 2000
112 z = float(indx-2)*c + c*(a - b(indx-1))/(b(indx) - b(indx-1))
Code:
do 110 i=1,2000 Did you mean Code:
do i=110,2000 |
| Jun28-11, 06:05 PM | #12 |
|
Mentor
|
What they removed from Fortran 95 was the assigned GOTO statement, which is described here - http://www.nsc.liu.se/~boein/f77to90/a2.html. That's not the same as the plain old GOTO statement.
The code that swarzism provided is an older style (Fortran 77?). The line number in the do statement refers to the line number of a continue statement. Code:
do 110 i = 1, 2000 ! some stuff 110 continue Code:
do i = 1, 2000 ! some stuff end do |
| Jun28-11, 07:13 PM | #13 |
|
|
About assigned goto statement, I'm not understanding the difference with the goto statement. For instance, Code:
goto (100, 156, 200), 2 Code:
goto 156 For the second, if I understand well, I could write Code:
goto 354 (15, 98, 354) Code:
goto 354 The first looks like allowed in Fortran 95 while the second not. Hmm I'm sure I don't get it. |
| Jun28-11, 11:21 PM | #14 |
|
|
A goto is just a form of flow-control that does not have context information. Let me explain what I mean. When you write branching code like an if statement: you can easily read the branching conditions in the if statement and see exactly what it is doing in terms of the criteria of the branch and change in flow control. You see what variables affect the change and you can follow what the program is doing. Its the same sort of argument with loops both conditional and unconditional. Same with recursive functions: in all these examples you are able to relate easily the flow control of the program with the state space that is being read or written. With the goto statement, it's hard to readily identify the type of context mentioned above, not only with respect to flow control in its own right (ifs, loops, select, case, function calls etc), but also its relation to the state space of the program. |
| Jun28-11, 11:59 PM | #15 |
|
Recognitions:
|
http://www.physicsforums.com/showthread.php?t=308782 |
| Jun29-11, 12:27 AM | #16 |
|
|
|
| Jun29-11, 09:36 AM | #17 |
|
|
The DO/CONTINUE format that I gave an example of is still used in 90,95,2003 and is even faster than DO/ENDDO. I wrote a small explanation why in the thread http://www.physicsforums.com/showthread.php?t=504768
|
| New Reply |
| Thread Tools | |
Similar Threads for: Goto statement, mostly regarding Fortran
|
||||
| Thread | Forum | Replies | ||
| Fortran 90, goto | Programming & Comp Sci | 13 | ||
| FORTRAN 77 read statement | Engineering, Comp Sci, & Technology Homework | 4 | ||
| FORTRAN Write Statement Issue | Programming & Comp Sci | 2 | ||
| meaning for fortran statement | Programming & Comp Sci | 1 | ||
| Fortran 90 very basic question : else if statement | Programming & Comp Sci | 6 | ||