From what I've read on this issue, I understand that the goto statement is discouraged for a few reasons:
(1) It leads to spaghetti code
(2) It makes the program hard to trace and modify
(3) Any program with a goto statement can be rewritten to not need one.
However, can someone show some examples of a good/legitimate uses of a goto statement? Meaning, a program or a segment of code where using a goto would be the best option.
Two examples that come to mind are parser generators (yacc) and state machine generators.
In both cases, you have a program generating C or C++ output. That program does consistency checks on the input such that if the input grammer is correct, a legal parser or state machine is generated. In other words, the gotos generated are under control of the generating program and the assumption is that the generating program knows how to use gotos correctly in the conext of the generated program.
While a yacc generated parser is in fact a state machine, the use of goto is somewhat different from how gotos are used in a generated state machine. With a yacc generated parser, gotos are generated to pass control to prefefined labels such as yyerror, when a syntax error is detected parsing the grammer. The code at yyerror is robust enough to try and recover from the error in the syntax it was parsing.
State machine generators operate a little differently. Each state in a state machine consts of one or more tuples of the following form:
<state> <event> <action> <next-state>
Depending on the state machine generator, <action> may be implemented as a jump to a user provided label, or it might be implemented as the address of a function. Code generated for the tuple can consist of a label for the state which is usually a jump table indexed by the event value. Likewise, <next-state> can be a goto the jump table for the next state.
I've had to use them several times over the past 28 years (jeez!) or so. For both of the reasons that AbstractAnon states and also for maths. Heavy maths that is, like fluid dynamics.
The goto command is more useful in C then it is in C++ where it can be used to jump around a function pretty much at will and do things like skip over cleanup that might not have to be done. There is some confusion out there where people don't realize that it behaves much differently in C++ in terms of flow control and the scope of automatic variables. Destructors don't get skipped when program flow jumps out of a variables scope, so it is not actually faster like some people try to believe. It exists primarily for backward compatibility, the supposed "valid" uses for it are sketchy at best. And to answer the question hanging in the air, no, I have no f'ing idea what AbstractionAnon is talking about :P .
(3) Any program with a goto statement can be rewritten to not need one.
This statement always gets me. C++ is a Turing complete language, there are a hundred valid answers to every trivial problem.
EDIT: Now that I think about it. I suppose the goto statement could come in handy when working with IDL's where you can't wrap an object with inheritance for some reason and so you can't "force" it to support RAII. But that feels like a greasy solution and I'm sure someone smarter then me can come up with a better one.
For the record, I have absolutely no clue what AbstractionAnon is talking about. Stuff seems way beyond me :)
@TheHardew: Is the correct answer "the first one"? I HAVE read that goto statements are useful for "pre-maturely" exiting loops. So is the reason for this simply that it requires less lines of code is "easier to read"? Seems like too trivial of a reason if you ask me.
Any other examples of code where a goto statement would come in handy or even be necessary? Would love to see more replies.
The problem I have with example #1 is that we no assurrance that there are not other jumps to "after_loop". In a small program or function, we can verify that visually. In a much larger program, that may not be so easy, especially if there are multiple "after_loop" labels (albeit in different functions). Using gotos is a bad habit to get into.
> In a much larger program, that may not be so easy, especially if there are multiple
> "after_loop" labels (albeit in different functions). Using gotos is a bad habit to get into.
One does not have to look beyond the the function in which the goto is present.
The scope of a label is the function in which it appears. - IS
Writing functions which are many dozens of lines long is arguably a worse habit to get into.
I've got to admit andywestken's example looks nice. Tough goto can be useful when you need to immediately exit the loops. If there was sth after 3rd loop you wouldn't like to execute you could make if(running) statements or just goto. In java there is some special type of break, if I remember correctly. It's much more comfortable than what we do in c++.
@AbstractionAnon Thankfully it's a rare case when you need to exit loop immediately.
> I've got to admit andywestken's example looks nice.
To me, it looks anything but nice; the version using goto is clearer, more readable, less grotesque.
Let me take this opportunity to remind people that
"being able to do something is not sufficient reason for doing it" and
"being able to do every trick is not a feature but a bug"
For the latter, remember Dijkstra's famous "Goto considered harmful" paper. The point was not that the "new features" (loop constructs) could do every goto trick better/simpler, but that some of those tricks should be avoided to simplify good programming.
Stroustrup in https://groups.google.com/a/isocpp.org/d/msg/concepts/IAHQkYcbCDc/nm8W86ozXQYJ
@ andywestken: IDL as in the later, Interface Description Language. I had a more specific example at the time and I want to say that it had something to do with a C based library, but it escapes me now. The reasoning was more to do with fewer lines of code then any observable difference in functionality.
Anyway, I stick with my original premise that 'goto' is, on the large, too misunderstood to justify being used in production code. Even though I harbor no doubt in my mind that Mr. JLBorges could take an up-turned plate of Alfredo and make it look like the Taj-Mahal by invoking this feature; I know at the same moment that I do not have to link to another of his posts for anyone here to understand the discrepancy between his own skill and that of the "average programmer".