GOTO
From Wikipedia, the free encyclopedia
- This page is about the programming command. See Goto for other uses.
GOTO is a statement found in many programming languages whose execution unconditionally causes the flow of control to be transferred to another point in the source code of a computer program. This point is specified using some kind of label, which may be an identifier or line number. At the machine code level a goto is a form branch statement.
GOTO is found in most high-level languages, including Ada, FORTRAN, Algol, COBOL, BASIC, Common Lisp, C, C++, C#, D, Pascal, Perl, Visual Basic, Batch (MS-DOS).
There are a few high-level languages that do not support a goto statement. For example, Java, where goto is a reserved word but does not presently serve any function. Hypertalk, TranScript (or Revolution), AppleScript, and Ruby do not have a goto.
SNOBOL does not have a goto statement, but it does support a form of statement suffix which causes an unconditional transfer of control after the statement has finished executing.
Contents |
[edit] Criticism
The GOTO statement has been the target of criticism, because if GOTOs are over-used it becomes very easy to produce unreadable and generally unmaintainable "spaghetti code". As structured programming became more prominent in the 1960s and 1970s, many computer scientists came to the conclusion that programs should always use so-called 'structured' flow-control commands such as loops and if-then-else statements in place of GOTO. However, others believed that even though the use of GOTO is often bad practice, there are some tasks that cannot be straightforwardly accomplished in many programming languages without the use of GOTO statements, such as breaking out of nested loops and exception handling.
One famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful [1]. In that letter Dijkstra argued that unrestricted GOTO statements should be abolished from higher-level languages because they complicated the task of analyzing and verifying the correctness of programs (particularly those involving loops). Donald Knuth's Structured Programming with go to Statements [2] considers some of the places where GOTO may be the appropriate tool. Generally these are in situations where a particular programming structure is not available. In these cases, GOTO can always be used to emulate the desired structure, since it is one of the fundamental building blocks of programming. Another solution to this problem is writing the desired control structure as a macro (one can do this in almost all Lisp dialects and in Forth).
[edit] Variations
A computed GOTO either jumps to one of several labels based on the value of an expression, or jumps to a label that has been stored in a variable. A computed GOTO compounds the problems of the simple GOTO, since a programmer cannot tell even by looking at the statement what will execute next. The ON GOTO statement in BASIC supports the first kind of computed GOTO and is useful for case-by-case branching, as in C's switch statement. Some C compilers support "goto" with a label variable using the label value operator. The label value operator && returns the address of its operand, which must be a label defined in the current function or a containing function. The value is a constant of type void* and should be used only in a computed goto statement. The feature is an extension to C and C++, implemented to facilitate porting programs developed with GNU C.[1]
A continuation is similar to a computed GOTO in that it transfers control from an arbitrary point in the program to a previously marked point. However, a continuation is more flexible than GOTO in many languages because it can leave the current function, while GOTO cannot. Executing a continuation usually involves some adjustment of the program's call stack in addition to a jump. The longjmp function of the C programming language is an example of an escape continuation that may be used to escape the current context to a surrounding one. The Common Lisp GO operator also has this stack unwinding property, despite the construct being lexically scoped, as the label to be jumped to can be referenced from a closure.
In the esoteric programming language INTERCAL, which is a parody of languages like BASIC, COME FROM is used instead of GOTO.
In Perl, there is a variant of the goto
statement that is not a traditional GOTO statement at all. It takes a function name and transfers control by effectively substituting one function call for another (a tail call): the new function will not return to the GOTO, but instead to the place from which the original function was called. Early versions of COBOL had the ALTER verb to this.
[edit] See also
[edit] References
- ^ Dijkstra, Edsger: Go To Statement Considered Harmful. Communications of the ACM 11:3 (1968), 147–148.
- ^ Knuth, Donald: Structured Programming with Goto Statements. Computing Surveys 6:4 (1974), 261–301.