Goto: Difference between revisions
Judas~enwiki (talk | contribs) m sadly adding PHP6 to the list of languages implementing this infamous statement... |
No edit summary |
||
Line 1: | Line 1: | ||
:''This page is about the programming command. See [[Goto]] for other uses.'' |
:''This page is about the programming command. See [[Goto]] for other uses.'' |
||
'''GOTO''' is a [[Statement (programming)|statement]] found in many [[programming language]]s |
'''GOTO''' is a [[Statement (programming)|statement]] found in many [[programming language]]s tjat instructs the computer to jump to another point in the [[computer program]], specified by a [[label]] or [[line number]]. It is the fundamental operation for transfer of control from one part of a program to another, since compilers translate other flow control statements into what are effectively GOTOs. |
||
GOTO is found in [[FORTRAN]], [[Algol programming language|Algol]], [[COBOL]], [[SNOBOL programming language|SNOBOL]], [[BASIC programming language|BASIC]], [[Common Lisp]], [[C programming language|C]], [[C++]], [[D programming language|D]], [[Pascal programming language|Pascal]], [[Perl]] , [[PHP|PHP6]] and many other languages, particularly [[assembly language]]s. In assembly language, the equivalent of the GOTO command is usually called BRA (from "branch"), JMP or JUMP. |
GOTO is found in [[FORTRAN]], [[Algol programming language|Algol]], [[COBOL]], [[SNOBOL programming language|SNOBOL]], [[BASIC programming language|BASIC]], [[Common Lisp]], [[C programming language|C]], [[C++]], [[D programming language|D]], [[Pascal programming language|Pascal]], [[Perl]] , [[PHP|PHP6]] and many other languages, particularly [[assembly language]]s. In assembly language, the equivalent of the GOTO command is usually called BRA (from "branch"), JMP or JUMP. |
Revision as of 07:36, 4 May 2006
- This page is about the programming command. See Goto for other uses.
GOTO is a statement found in many programming languages tjat instructs the computer to jump to another point in the computer program, specified by a label or line number. It is the fundamental operation for transfer of control from one part of a program to another, since compilers translate other flow control statements into what are effectively GOTOs.
GOTO is found in FORTRAN, Algol, COBOL, SNOBOL, BASIC, Common Lisp, C, C++, D, Pascal, Perl , PHP6 and many other languages, particularly assembly languages. In assembly language, the equivalent of the GOTO command is usually called BRA (from "branch"), JMP or JUMP.
However GOTO is not available in all high-level programming languages. For example, in Java "goto" is a reserved word but does not presently serve any function.
Criticism
The GOTO statement in high-level languages 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. 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 (see external links) 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).
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.
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.
In the programming language INTERCAL, which is a parody of languages like BASIC, COME FROM is used instead of GOTO.
In addition to the more typical use, Perl uses the GOTO statement as a way to call a subroutine using the current arguments held in the @_ variable.
See also
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.
Pop culture references
- monochrom's theatre play "Waiting for GOTO" ("Warten auf GOTO"), Volkstheater Wien, 2005.