Goto

From Wikipedia, the free encyclopedia

  (Redirected from GOTO)
Jump to: navigation, search

GOTO is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way jump to another line of code. The jumped-to locations are usually identified using labels, though some languages use line numbers. At the machine code level, a goto is a form of branch or jump statement.

In some languages, GOTO functionality may be present without explicit use of the keyword goto, such as where a break or continue keyword may be followed by an identifier denoting a label. The SNOBOL programming language supports a form of statement suffix which causes an unconditional transfer of control after the statement has finished executing.

Many languages support the goto statement, and many do not. In Java, goto is a reserved word, but is unusable.[1][2]

Contents

[edit] Usage

The goto statement is often combined with the if statement to cause a conditional transfer of control.

   IF condition THEN goto label;

Programming languages impose different restrictions with respect the jump location of a goto statement. For example, in the C programming language it is not allowed to jump to a label contained within another function.[3] The setjmp/longjmp functions provide support for non-local gotos.

[edit] Criticism of goto usage

Use of GOTO statements arguably results in "spaghetti code" that is difficult to read and maintain. However, there are arguably some tasks in some programming languages that cannot be accomplished straightforwardly without GOTO statements (such as: implementing finite state machines, breaking out of nested loops, and exception handling). The 1960s and 1970s saw computer scientists move away from GOTO statements in favor of the structured programming programming paradigm. Some programming style coding standards prohibit use of GOTO statements.

Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful.[4] 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). An alternative viewpoint is presented in Donald Knuth's Structured Programming with go to Statements [5] which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.

This criticism had an effect[citation needed] on the design of some programming languages. Although the designers of the Ada language in the late 1970s were aware of the criticisms of GOTO, the statement was still included in the language, mainly to support automatically generated code where the goto might prove indispensable.[6] However, the labels used as the destination of a goto statement take the unusual form of an identifier enclosed in double angle brackets (e.g. <<Start_Again>>) and this syntax is not used anywhere else in the language. This makes it easy to check a program for the existence of goto destinations. The goto statement itself takes the simple form goto Start_Again;.

[edit] Variations

There are a number of different language constructs which can be described as forms of goto:

[edit] Restricted GOTOs

Many languages, such as C and Java, provide related control flow statements, like break and continue, which are effectively restricted forms of the goto statement. Their effect is an unconditional jump, but they can only be used to jump to a point after the end of a loop block - either to continue a loop at the next iteration (continue), or to end the loop (break).

[edit] switch/case structures

The switch statement in C, C++ and Java effectively performs a multi-way goto where the destination is selected by the value of an expression. In some other languages the switch (or case) statement does not behave in precisely this way (it does not have "fall-through" behaviour), for example Limbo.

[edit] Computed GOTO

A computed GOTO (originally Fortran terminology) 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. 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.[7] Some C compilers (e.g., gcc) 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.[8]

Some variants of BASIC support a computed GOTO that can be any line number, not just one from a list. For example, one could write GOTO i*1000 to jump to the line numbered 1000 times the value of a variable i (which might represent a selected menu option, for example).

[edit] Continuations

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. A continuation can be more flexible than GOTO in some languages because it can leave the current function, something that a GOTO cannot do in most languages. 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.

[edit] "COME FROM" parody

In the esoteric programming language INTERCAL, which is a parody of languages like BASIC, COME FROM is used instead of GOTO.

[edit] Perl 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 accomplish this.

[edit] See also

[edit] References

  1. ^ "The Java Language Specification, Third Edition". http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.9. "The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs." 
  2. ^ "The Java Language Specification, Third Edition". http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.7. "Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement." 
  3. ^ C Standard section 6.8.6.1 The goto statement
  4. ^ Edsger Dijkstra (March 1968). "Go To Statement Considered Harmful". Communications of the ACM 11 (3): 147–148. doi:10.1145/362929.362947. http://portal.acm.org/citation.cfm?id=1241518&coll=ACM&dl=ACM&CFID=11218690&CFTOKEN=24551268. 
  5. ^ Donald Knuth (1974). "Structured Programming with go to Statements". Computing Surveys 6 (4): 261–301. doi:10.1145/356635.356640. http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf. 
  6. ^ John Barnes (2006-06-30). Programming in Ada 2005. Addison Wesley. p. 114–115. ISBN 0-32-134078-7. 
  7. ^ "Microsoft QuickBASIC: ON...GOSUB, ON...GOTO Statements QuickSCREEN.". Microsoft. 1988. http://www.qbasicnews.com/qboho/qckadvr@l804a.shtml. Retrieved on 2008-07-03. 
  8. ^ Computed goto, IBM XL C/C++ compiler

[edit] External links

Personal tools