Ultimately the use of goto statements in code is up to the programmer, especially if goto is part of the language (Java for example does NOT implement goto in the language API, where break and continue are implemented instead as methods of getting out of heavily nested loops and control structures. These two methods are also implemented in many languages). If the programmer, however has the problem of either maintaining or scaling their code because of gotos, then a rewrite is necessary. Loops and functions are nearly always scalable when well written.
Here are two documents that anybody should read if they have questions about goto statements:
- This one is about gotos being bad, by Edsger Dijkstra Go To Statement Considered Harmful
- This one is about gotos being good, by Donald Knuth Structured Programming With go to Statements
In general goto statements are not a part of object-oriented programming, because they break the object paradigm. They are however a part of structured programming, which generally the way one programs using C89 (ISO Standard C). But learning to use functions and loops provides for more readable code and higher reuse, as in the example code I supplied above. In general also, though goto statements may be usable, the number of encumbering labels may be more to blame for the unreadability of programs utilizing gotos heavily.
Thanks. I hope that we've all learned something in this thread, and that we continue to learn, because that's the only way we're all going to get better.
Errata:
[1] There is a LOOP opcode in most Assembly language implementations. It takes the form
- Code: Select all
LOOP label
Where label is similar to a goto label. link
Examples:
Goto example
- Code: Select all
label:
... /* Some code */
goto label;
Loop equivalent
- Code: Select all
while(condition)
{
... /* Some code */
}
or
- Code: Select all
for(init; condition; function)
{
... /* Some code */
}
or
- Code: Select all
do
{
... /* Some code */
} while(condition);

