Goto 197853 224001961 2008-07-06T22:06:07Z Lightbot 7178666 Units/dates/other {{otheruses|Goto (disambiguation)}} '''GOTO''' is a [[statement (programming)|statement]] found in many computer [[programming language]]s. It is a combination of the English words ''[[wiktionary:go|go]]'' and ''[[wiktionary:to|to]]''. When executed it causes an unconditional transfer of [[control flow|control]] (a "jump") to another statement. The jumped-to statement is specified using some kind of [[label (programming language)|label]], which may be an [[identifier]] or a [[line number]] depending on the language. At the [[machine code]] level a <tt>goto</tt> is a form of [[branch (computer science)|branch or jump statement]]. In some languages, <tt>goto</tt> functionality may be present without explicit use of the [[keyword (computer programming)|keyword]] <tt>goto</tt>, such as where a <tt>break</tt> or <tt>continue</tt> keyword may be followed by with 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. GOTO statements are found in most [[high-level language]]s. There are a few high-level languages that do not support a <tt>goto</tt> statement, for instance [[Java (programming language)|Java]] (where <tt>goto</tt> is a [[reserved word]] but does not presently serve any function). == Usage == The <tt>goto</tt> statement is often combined with the [[conditional (programming)|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 <code>goto</code> statement. For example, in the [[C programming language]] it is not allowed to jump to a label contained within another function.<ref>[http://c0x.coding-guidelines.com/6.8.6.1.html C Standard section 6.8.6.1 The goto statement]</ref> The [[setjmp|setjmp/longjmp]] functions provide support for non-local gotos. ==Criticism of goto usage== The GOTO statement has been the target of much continued criticism and debate, with the primary negative claim being that use of GOTO results in unreadable and generally unmaintainable "[[spaghetti code]]". As [[structured programming]] became more popular in the 1960s and 1970s, many [[computer scientist]]s came to the conclusion that programs should always use so-called 'structured' flow-control commands such as [[program loop|loops]] and if-then-else statements in place of GOTO. Even today some [[programming style]] coding standards forbid the use of GOTO statements using similar rationales. In defense of GOTO statements, others have noted that the restrained use of GOTO does not necessarily lead to poor quality code, and also argue that there are some tasks that cannot be straightforwardly accomplished in many programming languages without the use of one or more GOTO statements, such as implementing [[finite state machine]]s, breaking out of nested loops and [[exception handling]]. Probably the most famous criticism of GOTO is a 1968 letter by [[Edsger Dijkstra]] called ''Go To Statement [[Considered Harmful]]''.<ref>{{cite journal | author = [[Edsger Dijkstra]] | title = Go To Statement Considered Harmful | year = 1968 | month = March | journal = Communications of the ACM | volume = 11 | issue = 3 | url = http://portal.acm.org/citation.cfm?id=1241518&coll=ACM&dl=ACM&CFID=11218690&CFTOKEN=24551268 | pages = 147&ndash;148 | doi = 10.1145/362929.362947 }}</ref> 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'' <ref>{{cite journal | author = [[Donald Knuth]] | title = Structured Programming with Goto Statements | journal = Computing Surveys | volume = 6 | issue = 4 | year = 1974 | url = http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf | pages = 261–301 | doi = 10.1145/356635.356640 }}<!-- This link probably violated copyright --></ref> 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 on the design of some programming languages. Although the designers of the [[Ada (programming language)|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 <tt>goto</tt> might prove indispensable.<ref>{{cite book | title = Programming in Ada&nbsp;2005 | author = [[John Barnes (computer scientist)|John Barnes]] | page = 114&ndash;115 | date = 2006-06-30 | publisher = [[Addison Wesley]] | isbn = 0-32-134078-7 |}}</ref> 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. <tt><<Start_Again>></tt>) 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 <tt>'''goto''' Start_Again;</tt>. ==Variations== There are a number of different language constructs which can be described as forms of ''goto'': === Restricted GOTOs === Many languages, such as [[C (programming language)|C]] and [[Java (programming language)|Java]], provide related control flow statements, like <tt>break</tt> and <tt>continue</tt>, 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 [[iteration|loop]] block - either to continue a loop at the next iteration (continue), or to end the loop (break). === switch/case structures === The [[switch statement]] in [[C (programming language)|C]], [[C++ (programming language)|C++]] and [[Java (programming language)|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). === Computed GOTO === A '''computed <code>GOTO</code>''' (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 <code>ON ... GOTO</code> statement in [[BASIC]] supports the first kind of computed GOTO and is useful for case-by-case branching, as in C's [[switch statement]].<ref>{{cite web|url=http://www.qbasicnews.com/qboho/qckadvr@l804a.shtml|title= Microsot QuickBASIC: ON...GOSUB, ON...GOTO Statements QuickSCREEN.|date= 1988|accessdate= 2008-07-03|publisher=[[Microsoft]]}}</ref> Some C compilers (e.g., [[GNU Compiler Collection|gcc]]) support <code>goto</code> with a label variable using the '''label value operator'''. The ''label value operator'' <code>&&</code> 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 <code>void *</code> 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.<ref>[http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc08computed_goto.htm Computed goto], IBM XL C/C++ compiler</ref> === 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 [[Setjmp/longjmp|longjmp]] function of the [[C (programming language)|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 [[Lexical scope|lexically scoped]], as the label to be jumped to can be referenced from a [[Closure (computer science)|closure]]. === "COME FROM" GOTO parody === In the [[esoteric programming language]] [[INTERCAL programming language|INTERCAL]], which is a [[parody]] of languages like BASIC, [[Come from|COME FROM]] is used instead of GOTO. === Perl GOTO === In [[Perl]], there is a variant of the <code>goto</code> 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. ==See also== * [[Unstructured programming]] * [[Control flow#Goto|Control flow]] * [[GOSUB]] ==References== {{reflist}} == External links == * [http://www.geek-central.gen.nz/peeves/programming_discipline.html A Structured Discipline of Programming] [[Category:BASIC commands]] [[Category:Control flow]] [[ca:GOTO]] [[de:Sprunganweisung]] [[es:GOTO]] [[fr:Goto]] [[ko:GOTO문]] [[it:GOTO]] [[he:פקודת goto]] [[nl:GOTO]] [[ja:Goto文]] [[pl:Goto]] [[pt:Goto (programação)]] [[ru:GOTO]] [[sv:Goto]] [[tr:GOTO]] [[uk:Безумовний перехід]]