goto

{{Short description|One-way control statement in computer programming}}

{{About|the programming statement in high-level languages}}

File:GOTOkey(ZXSpectrum).jpg home computer, implemented with native BASIC (one-key command entry).]]

Goto is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control. 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 cases combined with a stack adjustment. Many languages support the goto statement, and many do not (see § language support).

The structured program theorem proved that the goto statement is not necessary to write programs that can be expressed as flow charts; some combination of the three programming constructs of sequence, selection/choice, and repetition/iteration are sufficient for any computation that can be performed by a Turing machine, with the caveat that code duplication and additional variables may need to be introduced.{{sfn|Watt|Findlay|2004}}

The use of goto was formerly common, but since the advent of structured programming in the 1960s and 1970s, its use has declined significantly. It remains in use in certain common usage patterns, but alternatives are generally used if available. In the past, there was considerable debate in academia and industry on the merits of the use of goto statements. The primary criticism is that code that uses goto statements is harder to understand than alternative constructions. Debates over its (more limited) uses continue in academia and software industry circles.

Usage

goto label

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 to the destination of a goto statement. For example, the C programming language does not permit a jump to a label contained within another function,{{sfn|Kernighan|Ritchie|1988|p=224|loc=A9.6 Jump Statements}} however jumps within a single call chain are possible using the setjmp/longjmp functions.

Criticism<!--'Go To Statement Considered Harmful' redirects here-->

At the pre-ALGOL meeting held in 1959, Heinz Zemanek explicitly cast doubt on the necessity of GOTO statements; at the time no one{{fact|date=November 2019}} paid attention to his remark, including Edsger W. Dijkstra, who later became the iconic opponent of GOTO.{{sfn|Dijkstra|1968}} The 1970s and 1980s saw a decline in the use of GOTO statements in favor of the structured programming paradigm, with GOTO criticized as leading to unmaintainable spaghetti code. Some programming style coding standards, for example the GNU Pascal Coding Standards, recommend against the use of GOTO statements.{{sfn|GNU Pascal development team|2005|loc=5.1 Assorted Pascal Programming Tips}} The Böhm–Jacopini proof (1966) did not settle the question of whether to adopt structured programming for software development, partly because the construction was more likely to obscure a program than to improve it because its application requires the introduction of additional local variables.{{sfn|Louden|Lambert|2012}} It did, however, spark a prominent debate among computer scientists, educators, language designers and application programmers that saw a slow but steady shift away from the formerly ubiquitous use of the GOTO. Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called "Go-to statement considered harmful".{{sfn|Dijkstra|1968}} 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)."The unbridled use of the goto statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The 'go to' statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program." The letter itself sparked a debate, including a {{"'}}GOTO Considered Harmful' Considered Harmful" letter{{sfn|Rubin|1987}} sent to Communications of the ACM (CACM) in March 1987, as well as further replies by other people, including Dijkstra's On a Somewhat Disappointing Correspondence.{{cite EWD|1009|On a Somewhat Disappointing Correspondence}} (May, 1987)

An alternative viewpoint is presented in Donald Knuth's Structured Programming with go to Statements, which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.{{sfn|Knuth|1974}} In The C Programming Language, Brian Kernighan and Dennis Ritchie warn that goto is "infinitely abusable", but also suggest that it could be used for end-of-function error handlers and for multi-level breaks from loops.{{sfn|Kernighan|Ritchie|1988|pp=65-66|loc=3.8 Goto and Labels}} These two patterns can be found in numerous subsequent books on C by other authors;{{sfn|Vine|2007|p=262}}{{sfn|Geisler|2011}}

{{sfn|Prata|2013}}{{sfn|Sahni|Cmelik|1995}} a 2007 introductory textbook notes that the error handling pattern is a way to work around the "lack of built-in exception handling within the C language".{{sfn|Vine|2007|p=262}} Other programmers, including Linux kernel designer and coder Linus Torvalds or software engineer and book author Steve McConnell, also object to Dijkstra's point of view, stating that GOTOs can be a useful language feature, improving program speed, size and code clarity, but only when used in a sensible way by a comparably sensible programmer.{{sfn|Andrews|2003}}{{sfn|McConnell|2004}} According to computer science professor John Regehr, in 2013, there were about 100,000 instances of goto in the Linux kernel code.{{sfn|Regehr|2013}}

Other academics took a more extreme viewpoint and argued that even instructions like break and return from the middle of loops are bad practice as they are not needed in the Böhm–Jacopini result, and thus advocated that loops should have a single exit point.{{sfn|Roberts|1995}} For instance, Bertrand Meyer wrote in his 2009 textbook that instructions like break and continue "are just the old goto in sheep's clothing".{{sfn|Meyer|2009}} A slightly modified form of the Böhm–Jacopini result, however, allows the avoidance of additional variables in structured programming, as long as multi-level breaks from loops are allowed.{{sfn|Kozen|Tseng|2008}} Because some languages like C don't allow multi-level breaks via their break keyword, some textbooks advise the programmer to use goto in such circumstances.{{sfn|Sahni|Cmelik|1995}} The MISRA C 2004 standard bans goto, continue, as well as multiple return and break statements.{{sfn|Stack Overflow Questions|2012}} The 2012 edition of the MISRA C standard downgraded the prohibition on goto from "required" to "advisory" status; the 2012 edition has an additional, mandatory rule that prohibits only backward, but not forward jumps with goto.{{sfn|Pitchford|Tapp|2013}}{{sfn|Williams|2013}}

FORTRAN introduced structured programming constructs in 1978, and in successive revisions the relatively loose semantic rules governing the allowable use of goto were tightened; the "extended range" in which a programmer could use a GOTO to leave and re-enter a still-executing DO loop was removed from the language in 1978,ANSI X3.9-1978. American National Standard – Programming Language FORTRAN. American National Standards Institute. Also known as ISO 1539-1980, informally known as FORTRAN 77 and by 1995 several forms of Fortran GOTO, including the Computed GOTO and the Assigned GOTO, had been deleted.ISO/IEC 1539-1:1997. Information technology – Programming languages – Fortran – Part 1: Base language. Informally known as Fortran 95. There are a further two parts to this standard. Part 1 has been formally adopted by ANSI. Some widely used modern programming languages such as Java and Python lack the GOTO statement – see language support – though most provide some means of breaking out of a selection, or either breaking out of or moving on to the next step of an iteration. The viewpoint that disturbing the control flow in code is undesirable may be seen in the design of some programming languages, for instance Ada{{sfn|Barnes|2006}} visually emphasizes label definitions using angle brackets.

Entry 17.10 in comp.lang.c FAQ list{{sfn|Summit|1995}} addresses the issue of GOTO use directly, stating

{{Quotation| Programming style, like writing style, is somewhat of an art and cannot be codified by inflexible rules, although discussions about style often seem to center exclusively around such rules. In the case of the goto statement, it has long been observed that unfettered use of goto's quickly leads to unmaintainable spaghetti code. However, a simple, unthinking ban on the goto statement does not necessarily lead immediately to beautiful programming: an unstructured programmer is just as capable of constructing a Byzantine tangle without using any goto's (perhaps substituting oddly-nested loops and Boolean control variables, instead). Many programmers adopt a moderate stance: goto's are usually to be avoided, but are acceptable in a few well-constrained situations, if necessary: as multi-level break statements, to coalesce common actions inside a switch statement, or to centralize cleanup tasks in a function with several error returns. (...) Blindly avoiding certain constructs or following rules without understanding them can lead to just as many problems as the rules were supposed to avert. Furthermore, many opinions on programming style are just that: opinions. They may be strongly argued and strongly felt, they may be backed up by solid-seeming evidence and arguments, but the opposing opinions may be just as strongly felt, supported, and argued. It's usually futile to get dragged into "style wars", because on certain issues, opponents can never seem to agree, or agree to disagree, or stop arguing.}}

Common usage patterns

While overall usage of goto has been declining, there are still situations in some languages where a goto provides the shortest and most straightforward way to express a program's logic (while it is possible to express the same logic without gotos, the equivalent code will be longer and often more difficult to understand). In other languages, there are structured alternatives, notably exceptions and tail calls.

Situations in which goto is often useful include:

  • To make the code more readable and easier to follow{{sfn|Andrews|2003}}{{sfn|Torvalds|2016}}
  • To make smaller programs, and get rid of code duplication{{sfn|Andrews|2003}}{{sfn|Torvalds|2016}}
  • Implement a finite-state machine, using a state transition table and goto to switch between states (in absence of tail call elimination), particularly in automatically generated C code.{{sfn|Cozens|2004}} For example, goto in the canonical LR parser.
  • Implementing multi-level break and continue if not directly supported in the language; this is a common idiom in C.{{sfn|Sahni|Cmelik|1995}} Although Java reserves the goto keyword, it doesn't actually implement it. Instead, Java implements labelled break and labelled continue statements.{{sfn|Java Tutorial|2012}} According to the Java documentation, the use of gotos for multi-level breaks was the most common (90%) use of gotos in C.{{sfn|Gosling|McGilton|1996}} Java was not the first language to take this approach—forbidding goto, but providing multi-level breaks— the BLISS programming language (more precisely the BLISS-11 version thereof) preceded it in this respect.{{sfn|Brender|2002|pp=960–965}}
  • Surrogates for single-level break or continue (retry) statements when the potential introduction of additional loops could incorrectly affect the control flow. This practice has been observed in Netbsd code.{{sfn|Spinellis|2003}}
  • Error handling (in absence of exceptions), particularly cleanup code such as resource deallocation.{{sfn|Vine|2007|p=262}}{{sfn|Sahni|Cmelik|1995}}{{sfn|Spinellis|2003}}{{sfn|Cozens|2004}}{{sfn|Allain|2019}} C++ offers an alternative to goto statement for this use case, which is : Resource Acquisition Is Initialization (RAII) through using destructors or using try and catch exceptions used in Exception handling.{{sfn|Stroustrup|2012}} setjmp and longjmp are another alternative, and have the advantage of being able to unwind part of the call stack.
  • Popping the stack in, e.g., Algol, PL/I.
  • Specialized scripting languages that operate in a linear manner, such as a dialogue system for video games.{{cite web |last1=Hoad |first1=Nathan |title=nathanhoad/godot_dialogue_manager |website=GitHub |url=https://github.com/nathanhoad/godot_dialogue_manager/blob/main/docs/Writing_Dialogue.md |access-date=3 February 2023 |date=28 July 2022}}

These uses are relatively common in C, but much less common in C++ or other languages with higher-level features.{{sfn|Allain|2019}} However, throwing and catching an exception inside a function can be extraordinarily inefficient in some languages; a prime example is Objective-C, where a goto is a much faster alternative.{{sfn|Chisnall|2012}}

Another use of goto statements is to modify poorly factored legacy code, where avoiding a goto would require extensive refactoring or code duplication. For example, given a large function where only certain code is of interest, a goto statement allows one to jump to or from only the relevant code, without otherwise modifying the function. This usage is considered code smell,{{sfn|Contieri|2021}} but finds occasional use.

Alternatives

=Structured programming=

The modern notion of subroutine was invented by David Wheeler when programming the EDSAC. To implement a call and return on a machine without a subroutine call instruction, he used a special pattern of self-modifying code, known as a Wheeler jump.{{sfn|Wilkes|Wheeler|Gill|1951}} This resulted in the ability to structure programs using well-nested executions of routines drawn from a library. This would not have been possible using only goto, since the target code, being drawn from the library, would not know where to jump back to.

Later, high-level languages such as Pascal were designed around support for structured programming, which generalized from subroutines (also known as procedures or functions) towards

further control structures such as:

These new language mechanisms replaced equivalent flows which previously would have been written using gotos and ifs. Multi-way branching replaces the "computed goto" in which the instruction to jump to is determined dynamically (conditionally).

Under certain conditions it is possible to eliminate local goto statements of legacy programs by replacing them with multilevel loop exit statements.{{sfn|Ramshaw|1988}}

=Exceptions=

{{further|Exception handling}}

In practice, a strict adherence to the basic three-structure template of structured programming yields highly nested code, due to inability to exit a structured unit prematurely, and a combinatorial explosion with quite complex program state data to handle all possible conditions.

Two solutions have been generally adopted: a way to exit a structured unit prematurely, and more generally exceptions – in both cases these go up the structure, returning control to enclosing blocks or functions, but do not jump to arbitrary code locations. These are analogous to the use of a return statement in non-terminal position – not strictly structured, due to early exit, but a mild relaxation of the strictures of structured programming. In C, break and continue allow one to terminate a loop or continue to the next iteration, without requiring an extra while or if statement. In some languages multi-level breaks are also possible. For handling exceptional situations, specialized exception handling constructs were added, such as try/catch/finally in Java.

The throw-catch exception handling mechanisms can also be easily abused to create non-transparent control structures, just like goto can be abused.{{sfn|Siedersleben|2006}}

=Tail calls=

{{Main article|Tail call}}

In a paper delivered to the ACM conference in Seattle in 1977, Guy L. Steele summarized the debate over the GOTO and structured programming, and observed that procedure calls in the tail position of a procedure can be most optimally treated as a direct transfer of control to the called procedure, typically eliminating unnecessary stack manipulation operations.{{sfn|Steele|1977}} Since such "tail calls" are very common in Lisp, a language where procedure calls are ubiquitous, this form of optimization considerably reduces the cost of a procedure call compared to the GOTO used in other languages. Steele argued that poorly implemented procedure calls had led to an artificial perception that the GOTO was cheap compared to the procedure call. Steele further argued that "in general procedure calls may be usefully thought of as GOTO statements which also pass parameters, and can be uniformly coded as machine code JUMP instructions", with the machine code stack manipulation instructions "considered an optimization (rather than vice versa!)".{{sfn|Steele|1977}} Steele cited evidence that well optimized numerical algorithms in Lisp could execute faster than code produced by then-available commercial Fortran compilers because the cost of a procedure call in Lisp was much lower. In Scheme, a Lisp dialect developed by Steele with Gerald Jay Sussman, tail call optimization is mandatory.{{sfn|Kelsey|Clinger|Rees|1998}}

Although Steele's paper did not introduce much that was new to computer science, at least as it was practised at MIT, it brought to light the scope for procedure call optimization, which made the modularity-promoting qualities of procedures into a more credible alternative to the then-common coding habits of large monolithic procedures with complex internal control structures and extensive state data. In particular, the tail call optimizations discussed by Steele turned the procedure into a credible way of implementing iteration through single tail recursion (tail recursion calling the same function). Further, tail call optimization allows mutual recursion of unbounded depth, assuming tail calls – this allows transfer of control, as in finite-state machines, which otherwise is generally accomplished with goto statements.

=Coroutines=

{{main article|Coroutine}}

Coroutines are a more radical relaxation of structured programming, allowing not only multiple exit points (as in returns in non-tail position), but also multiple entry points, similar to goto statements. Coroutines are more restricted than goto, as they can only resume a currently running coroutine at specified points – continuing after a yield – rather than jumping to an arbitrary point in the code. A limited form of coroutines are generators, which are sufficient for some purposes. Even more limited are closures – subroutines which maintain state (via static variables), but not execution position. A combination of state variables and structured control, notably an overall switch statement, can allow a subroutine to resume execution at an arbitrary point on subsequent calls, and is a structured alternative to goto statements in the absence of coroutines; this is a common idiom in C, for example.

=Continuations=

{{main article|Continuation}}

A continuation is similar to a GOTO in that it transfers control from an arbitrary point in the program to a previously marked point. A continuation is more flexible than GOTO in those languages that support it, because it can transfer control out of the current function, something that a GOTO cannot do in most structured programming languages. In those language implementations that maintain stack frames for storage of local variables and function arguments, executing a continuation involves adjusting 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 Scheme, continuations can even move control from an outer context to an inner one if desired. This almost limitless control over what code is executed next makes complex control structures such as coroutines and cooperative multitasking relatively easy to write.{{sfn|Kelsey|Clinger|Rees|1998}}

=Message passing=

{{main article|Message passing}}

In non-procedural paradigms, goto is less relevant or completely absent. One of the main alternatives is message passing, which is of particular importance in concurrent computing, interprocess communication, and object oriented programming. In these cases, the individual components do not have arbitrary transfer of control, but the overall control may be scheduled in complex ways, such as via preemption. The influential languages Simula and Smalltalk were among the first to introduce the concepts of messages and objects. By encapsulating state data, object-oriented programming reduced software complexity to interactions (messages) between objects.

Variations

There are a number of different language constructs under the class of goto statements.

={{visible anchor|Computed GOTO}} and {{visible anchor|Assigned GOTO}}=

{{See also|Branch table}}

In Fortran, a computed GOTO jumps to one of several labels in a list, based on the value of an expression. An example is goto (20,30,40) i., which means that the program jumps to label 20, 30 or 40, in case that i is either less than, equal to or greater than zero. The equivalent construct in C is the switch statement, and in newer Fortran a SELECT CASE construct is the recommended syntactical alternative.{{sfn|Lahey Computer Systems, Inc|2004}} BASIC had a 'On GoTo' statement that achieved the same goal, but in Visual Basic this construct is no longer supported.{{sfn|Microsoft|2021|}}

In versions prior to Fortran 95, Fortran also had an assigned goto variant that transfers control to a statement label (line number) which is stored in (assigned to) an integer variable. Jumping to an integer variable that had not been ASSIGNed to was unfortunately possible, and was a major source of bugs involving assigned gotos.{{sfn|Wehr|1997}} The Fortran assign statement only allows a constant (existing) line number to be assigned to the integer variable. However, some compilers allowed accidentally treating this variable as an integer thereafter, for example increment it, resulting in unspecified behavior at goto time. The following code demonstrates the behavior of the goto i when line i is unspecified:

assign 200 to i

i = i+1

goto i ! unspecified behavior

200 write(*,*) "this is valid line number"

Several C compilers implement two non-standard C/C++ extensions relating to gotos originally introduced by gcc.{{sfn|z/OS 2.5.0 in IBM Documentation|2021}} The GNU extension allows the address of a label inside the current function to be obtained as a void* using the unary, prefix label value operator &&. The goto instruction is also extended to allow jumping to an arbitrary void* expression. This C extension is referred to as a computed goto in documentation of the C compilers that support it; its semantics are a superset of Fortran's assigned goto, because it allows arbitrary pointer expressions as the goto target, while Fortran's assigned goto doesn't allow arbitrary expressions as jump target.{{sfn|GCC, the GNU Compiler Collection|2021}} As with the standard goto in C, the GNU C extension allows the target of the computed goto to reside only in the current function. Attempting to jump outside the current function results in unspecified behavior.{{sfn|GCC, the GNU Compiler Collection|2021}}

Some variants of BASIC also support a computed GOTO in the sense used in GNU C, i.e. in which the target can be any line number, not just one from a list. For example, in MTS BASIC 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).{{sfn|Fronczak|Lubbers|1974|p=226}}

PL/I label variables achieve the effect of computed or assigned GOTOs.

=ALTER=

Up to the 1985 ANSI COBOL standard had the ALTER statement which could be used to change the destination of an existing GO TO, which had to be in a paragraph by itself.The ALTER statement was deemed obsolete in the COBOL 1985 standard and deleted in 2002; see COBOL > Self-modifying code The feature, which allowed polymorphism, was frequently condemned and seldom used.{{sfn|Van Tassel|2004}}

=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.{{sfn|Perl syntax manual|2021}}

=Emulated GOTO=

There are several programming languages that do not support GOTO by default. By using GOTO emulation, it is still possible to use GOTO in these programming languages, albeit with some restrictions. One can emulate GOTO in Java,{{sfn|GOTO for Java|2009}} JavaScript,{{sfn|Sexton|2012}} and Python.{{sfn|Hindle|2004}}{{sfn|Noack|Blank|Grainger|spacewander|2015}}

=PL/I label variables=

PL/I has the data type LABEL, which can be used to implement both the "assigned goto" and the "computed goto." PL/I allows branches out of the current block. A calling procedure can pass a label as an argument to a called procedure which can then exit with a branch. The value of a label variable includes the address of a stack frame, and a goto out of block pops the stack.

/* This implements the equivalent of */

/* the assigned goto */

declare where label;

where = somewhere;

goto where;

...

somewhere: /* statement */ ;

...

/* This implements the equivalent of */

/* the computed goto */

declare where (5) label;

declare inx fixed;

where(1) = abc;

where(2) = xyz;

...

goto where(inx);

...

abc: /* statement */ ;

...

xyz: /* statement */ ;

...

A simpler way to get an equivalent result is using a label constant array that doesn't even need an explicit declaration of a LABEL type variable:

/* This implements the equivalent of */

/* the computed goto */

declare inx fixed;

...

goto where(inx);

...

where(1): /* statement */ ;

...

where(2): /* statement */ ;

...

=MS/DOS GOTO=

In a DOS batch file, Goto directs execution to a label that begins with a colon.

The target of the Goto can be a variable.

@echo off

SET D8str=%date%

SET D8dow=%D8str:~0,3%

FOR %%D in (Mon Wed Fri) do if "%%D" == "%D8dow%" goto SHOP%%D

echo Today, %D8dow%, is not a shopping day.

goto end

:SHOPMon

echo buy pizza for lunch - Monday is Pizza day.

goto end

:SHOPWed

echo buy Calzone to take home - today is Wednesday.

goto end

:SHOPFri

echo buy Seltzer in case somebody wants a zero calorie drink.

:end

Language support

Many languages support the goto statement, and many do not. In Java, goto is a reserved word, but is unusable, although compiled .class files generate GOTOs and LABELs.{{harvtxt|Gosling|Joy|Steele|Bracha|2005}} 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. 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. Python does not have support for goto, although there are several joke modules that provide it.{{sfn|Hindle|2004}}{{sfn|Noack|Blank|Grainger|spacewander|2015}} There is no goto statement in Seed7 and hidden gotos like break- and continue-statements are also omitted.{{sfn|Manual for the Seed7 programming language|2021}} In PHP there was no native support for goto until version 5.3 (libraries were available to emulate its functionality).{{sfn|PHP Manual|2021}}

C# and Visual Basic .NET both support goto.{{sfn|Wagner|2021}}{{cite web |title = GoTo Statement - Visual Basic | Microsoft Learn |url = https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/goto-statement |website = Microsoft Learn |date = 15 September 2021 |access-date = 25 September 2023}} However, it does not allow jumping to a label outside of the current scope, and respects object disposal and finally constructs, making it significantly less powerful and dangerous than the goto keyword in other programming languages. It also makes case and default statements labels, whose scope is the enclosing switch statement; goto case or goto default is often used as an explicit replacement for implicit fallthrough, which C# disallows.

The PL/I programing language has a GOTO statement that unwinds the stack for an out of block transfer and does not permit a transfer into a block from outside of it.

Other languages may have their own separate keywords for explicit fallthroughs, which can be considered a version of goto restricted to this specific purpose. For example, Go uses the fallthrough keyword and doesn't allow implicit fallthrough at all,{{sfn|The Go Programming Language Specification|2021}} while Perl 5 uses next for explicit fallthrough by default, but also allows setting implicit fallthrough as default behavior for a module.

Most languages that have goto statements call it that, but in the early days of computing, other names were used. For example, in MAD the TRANSFER TO statement was used.{{sfn|Galler|1962|pp=26-28, 197, 211}} APL uses a right pointing arrow, for goto.

C has goto, and it is commonly used in various idioms, as discussed above.

Functional programming languages such as Scheme generally do not have goto, instead using continuations.

See also

Notes

{{reflist|colwidth=30em}}

References

{{refbegin}}

  • {{cite web

|last= Allain

|first= Alex

|url= http://www.cprogramming.com/tutorial/goto.html

|title= When To Use Goto in C

|year= 2019

|access-date= 2021-11-14

}}

  • {{cite web

|last= Andrews

|first= Jeremy

|url= http://kerneltrap.org/node/553/2131

|archive-date= 28 November 2005

|title= Linux: Using goto In Kernel Code

|date= 13 January 2003

|archive-url= https://web.archive.org/web/20051128093253/http://kerneltrap.org/node/553/2131

|access-date= 2021-11-14

}}

  • {{cite book

|last= Barnes

|first= John

|author-link= John Barnes (computer scientist)

|title= Programming in Ada 2005

|page= 114-115

|date= 2006-06-30

|publisher= Addison Wesley

|isbn= 978-0-321-34078-8

}}

  • {{cite journal

|last= Brender

|first= Ronald F.

|doi= 10.1002/spe.470

|title= The BLISS programming language: a history

|journal= Software: Practice and Experience

|date= 2002

|volume= 32

|issue= 10

|pages= 955–981

|s2cid= 45466625

|url= https://www.cs.tufts.edu/~nr/cs257/archive/ronald-brender/bliss.pdf

}}

  • {{cite book

|last= Chisnall

|first= David

|title= Objective-C Phrasebook

|url= https://archive.org/details/objectivecphrase0000chis

|url-access= registration

|year= 2012

|publisher= Addison-Wesley Professional

|isbn= 978-0-321-81375-6

|page= [https://archive.org/details/objectivecphrase0000chis/page/249 249]

}}

  • {{Cite web

|last=Contieri

|first=Maxi

|title=Code Smell 100 - GoTo

|url=https://maximilianocontieri.com/code-smell-100-goto

|date=2021-11-02

|access-date=2021-11-14

|website=Maximiliano Contieri - Software Design

|language=en

}}

  • {{Cite web

|last= Cozens

|first= Simon

|url= http://www.simon-cozens.org/content/good-uses-goto

|title= Good uses of goto

|date= 2004-04-16

|archive-url= https://web.archive.org/web/20110319021034/http://www.simon-cozens.org/content/good-uses-goto

|archive-date= 2011-03-19

}}

  • {{Cite journal

|last= Dijkstra

|first= Edsger W.

|author-link= Edsger Dijkstra

|title= Letters to the editor: Go to statement considered harmful

|date= March 1968

|doi= 10.1145/362929.362947

|url= https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

|journal= Communications of the ACM

|volume= 11

|issue= 3

|pages= 147–148

|s2cid= 17469809

}}

  • {{cite EWD|1009|On a Somewhat Disappointing Correspondence}}
  • {{cite book

|last1= Fronczak

|first1= Edward J.

|last2= Lubbers

|first2= Clark E.

|publisher= University of Michigan Computing Center

|title= MTS, Michigan Terminal System

|url= https://books.google.com/books?id=_wLvAAAAMAAJ&pg=PA226

|date= September 1974

|id= UOM:39015034770076

}}

  • {{cite book

|last= Galler

|first= Bernard A.

|title= The Language of Computers

|url= http://www.bitsavers.org/pdf/univOfMichigan/mad/Galler_TheLangOfComps_1962.pdf

|date= January 1, 1962

|publisher= McGraw-Hill

}}

  • {{cite web

|author= GCC, the GNU Compiler Collection

|url= https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

|title= Labels as Values - Using the GNU Compiler Collection (GCC)

|publisher= Gcc.gnu.org

|year= 2021

|access-date= 2021-11-13

}}

  • {{cite book

|last= Geisler

|first= Sandra

|title= C All-in-One Desk Reference For Dummies

|year= 2011

|publisher= John Wiley & Sons

|isbn= 978-1-118-05424-6

|pages= 217–220

}}

  • {{cite web

|author= GNU Pascal development team

|author-link= Free Software Foundation

|url= http://www.gnu-pascal.de/h-gpcs-en.html#Assorted-Tips

|title= GNU Pascal Coding Standards

|date= 2005

|publisher= Free Software Foundation

|website= www.gnu-pascal.de

|access-date= 2021-11-10

}}

  • {{cite web

|last1= Gosling

|first1= James

|author-link1= James Gosling

|last2= McGilton

|first2= Henry

|url= http://www.oracle.com/technetwork/java/simple-142616.html

|title= The Java Language Environment

|publisher= Oracle.com

|date= May 1996

|access-date= 2014-07-22

}}

  • {{cite book

|last1= Gosling

|first1= James

|last2= Joy

|first2= Bill

|author-link2= Bill Joy

|last3= Steele

|first3= Guy Lewis

|author-link3= Guy L. Steele, Jr.

|last4= Bracha

|first4= Gilad

|author-link4= Gilad Bracha

|title= The Java Language Specification

|url= https://java.sun.com/docs/books/jls/index.html

|edition= 3rd

|year= 2005

|publisher= Addison-Wesley

|isbn= 0-321-24678-0

|access-date= February 8, 2019

|archive-date= February 14, 2012

|archive-url= https://web.archive.org/web/20120214061826/http://java.sun.com/docs/books/jls/index.html

|url-status= live

}}

  • {{cite web

|author= GOTO for Java

|url= http://www.steike.com/code/useless/java-goto/

|title= GOTO for Java

|work= steik

|date= July 6, 2009

|access-date= April 28, 2012

|archive-url= https://web.archive.org/web/20120615003103/http://www.steike.com/code/useless/java-goto/

|archive-date= June 15, 2012

|url-status= dead

}}

  • {{cite web

|last= Hindle

|first= Richie

|url= http://entrian.com/goto/

|title= goto for Python

|work= Entrian Solutions

|date= April 1, 2004

|access-date= 2021-11-10

|location= Hertford, UK

|publisher= Entrian Solutions Ltd

}}

  • {{cite web

|author= Java Tutorial

|url= http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

|title= Branching Statements (The Java Tutorials > Learning the Java Language > Language Basics)

|publisher= Docs.oracle.com

|date= 2012-02-28

|access-date= 2021-11-10

}}

  • {{cite journal

|last1= Kelsey

|first1= R.

|last2= Clinger

|first2= W.

|last3= Rees

|first3= J.

|date= August 1998

|title= Revised5 Report on the Algorithmic Language Scheme

|url= http://www.schemers.org/Documents/Standards/R5RS/

|journal= Higher-Order and Symbolic Computation

|volume= 11

|issue= 1

|pages= 7–105

|doi= 10.1023/A:1010051815785

|s2cid= 14069423

|display-authors= etal

|url-access= subscription

}}

  • {{cite book

|last1= Kernighan

|first1= Brian Wilson

|author-link1= Brian Kernighan

|last2= Ritchie

|first2= Dennis MacAlistair

|author-link2= Dennis Ritchie

|title= C Programming Language

|url= https://archive.org/details/cprogramminglang00bria

|url-access= registration

|year= 1988

|publisher= Prentice Hall

|isbn= 978-0-13-308621-8

|edition= 2nd

}}

  • {{cite journal

| last= Knuth

| first= Donald

| author-link= Donald Knuth

| title= Structured Programming with go to Statements

| journal= Computing Surveys

| volume= 6

| issue= 4

| year= 1974

| url= https://www.cs.sjsu.edu/~mak/CS185C/KnuthStructuredProgrammingGoTo.pdf

| pages= 261–301

| doi= 10.1145/356635.356640

| citeseerx= 10.1.1.103.6084

| s2cid= 207630080

| access-date= 2017-01-26

| archive-date= 2017-07-17

| archive-url= https://web.archive.org/web/20170717042242/https://www.cs.sjsu.edu/~mak/CS185C/KnuthStructuredProgrammingGoTo.pdf

| url-status= dead

}}

  • {{cite conference

|conference= 9th International Conference MPC 2008

|book-title= Mathematics of Program Construction

|editor-last1 = Audebaud

|editor-first1 = Philippe

|editor-last2 = Paulin-Mohring

|editor-first2 = Christine

|title= The Böhm_Jacopini Theorem Is False, Propositionally

|last1= Kozen

|first1= Dexter

|author-link1= Dexter Kozen

|last2= Tseng

|first2= Wei-Lung Dustin

|url= http://www.cs.cornell.edu/~kozen/papers/bohmjacopini.pdf

|doi= 10.1007/978-3-540-70594-9_11 |location= Marseille France

|volume= 5133

|pages= 177_192

|series= Lecture Notes in Computer Science

|date= July 2008

|isbn= 978-3-540-70593-2

|citeseerx= 10.1.1.218.9241

}}

  • {{cite web

|author= Lahey Computer Systems, Inc

|url= http://www.lahey.com/docs/lfprohelp/F95ARComputed_GOTOStmt.htm

|title= Computed GOTO Statement (obsolescent)

|publisher= Lahey Computer Systems, Inc.

|year= 2004

|access-date= 2021-11-10

|archive-url= https://web.archive.org/web/20160526142532/http://www.lahey.com/docs/lfprohelp/F95ARComputed_GOTOStmt.htm

|archive-date= 2016-05-26

}}

  • {{cite book

|last1= Louden

|first1= Kenneth C.

|last2= Lambert

|first2= Kenneth A.

|title= Programming Languages: Principles and Practices

|url= https://archive.org/details/programminglangu00loud_140

|url-access= limited

|year= 2012

|publisher= Cengage Learning

|isbn= 978-1-111-52941-3

|page= [https://archive.org/details/programminglangu00loud_140/page/n426 422]

}}

  • {{cite web

|author= Manual for the Seed7 programming language

|url= http://seed7.sourceforge.net/manual/intro.htm#Features_of_Seed7

|title= Features of Seed7

|year= 2021

|access-date= 2021-11-10

}}

  • {{cite book

|last= McConnell

|first= Steve

|edition= 2nd

|title= Code Complete: A Practical Handbook of Software Construction, Second Edition

|publisher= Microsoft Press

|date= December 2004

|isbn= 978-0735619678

}}

  • {{cite book

|last= Meyer

|first= Bertrand

|title= Touch of Class: Learning to Program Well with Objects and Contracts

|year= 2009

|publisher= Springer Science & Business Media

|isbn= 978-3-540-92144-8

|page= 189

}}

  • {{cite web

|author= Microsoft

|author-link= Microsoft

|url= https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc30817

|title= 'On GoTo' and 'On GoSub' statements are no longer supported

|year= 2021

|access-date= 2021-11-10

|publisher= Microsoft

}}

  • {{cite web

|last1= Noack

|first1= Sebastian

|last2= Blank

|first2= Douglas

|last3= Grainger

|first3= Thomas

|last4= spacewander

|url= https://github.com/snoack/python-goto

|title= snoack/python-goto: A function decorator, that rewrites the bytecode, to enable goto in Python

|website= GitHub

|date= September 19, 2015

|access-date= 2021-11-10

}}

  • {{cite report

|author= Perl syntax manual

|url= https://perldoc.perl.org/perlsyn#Goto

|title= Goto

|year= 2021

|access-date= 2021-11-14

}}

  • {{cite web

|author= PHP Manual

|url= https://www.php.net/manual/en/control-structures.goto.php

|title= goto

|publisher= PHP

|year= 2021

|access-date= 2021-11-13

}}

  • {{cite web

|last1= Pitchford

|first1= Mark

|last2= Tapp

|first2= Chris

|url= http://electronicdesign.com/dev-tools/misra-c2012-plenty-good-reasons-change

|title= MISRA C:2012: Plenty Of Good Reasons To Change

|publisher= Electronic Design

|date= 2013-02-25

|access-date= 2014-07-22

}}

  • {{cite book

|last= Prata

|first= Stephen

|title= C Primer Plus

|year= 2013

|publisher= Addison-Wesley

|isbn= 978-0-13-343238-1

|pages= 287–289

}}

  • {{Cite journal

|last= Ramshaw

|first= L.

|doi= 10.1145/48014.48021

|title= Eliminating go to's while preserving program structure

|journal= Journal of the ACM

|volume= 35

|issue= 4

|pages= 893–920

|year= 1988

|s2cid= 31001665

|doi-access= free

}}

  • {{cite web

|last= Regehr

|first= John

|author-link= John Regehr

|url= http://blog.regehr.org/archives/894

|title= Use of Goto in Systems Code – Embedded in Academia

|date= February 4, 2013

|website= blog.regehr.org

}}

  • {{cite journal

|last= Roberts

|first= Eric S.

|title= Loop exits and structured programming: reopening the debate

|journal= ACM SIGCSE Bulletin

|date= March 1995

|volume= 27

|issue= 1

|pages= 268–272

|doi= 10.1145/199691.199815

|doi-access= free

}}

  • {{cite journal

|last= Rubin

|first= Frank

|date= March 1987

|url= http://www.ecn.purdue.edu/ParaMount/papers/rubin87goto.pdf

|archive-url= https://web.archive.org/web/20090320002214/http://www.ecn.purdue.edu/ParaMount/papers/rubin87goto.pdf

|archive-date= 2009-03-20

|title= "GOTO Considered Harmful" Considered Harmful

|journal= Communications of the ACM

|volume= 30

|issue= 3

|pages= 195–196

|doi= 10.1145/214748.315722

|s2cid= 6853038

}}

  • {{cite book

|last1= Sahni

|first1= Sartaj

|last2= Cmelik

|first2= Bob

|title= Software Development in C

|url= https://books.google.com/books?id=78hu9aMNMZQC&pg=PA135

|year= 1995

|publisher= Silicon Press

|isbn= 978-0-929306-16-2

|page= 135

}}

  • {{cite web

|last= Sexton

|first= Alex

|url= http://summerofgoto.com/

|title= The Summer of Goto | Official Home of Goto.js

|year= 2012

|archive-date= October 25, 2015

|archive-url= https://web.archive.org/web/20151025183831/http://www.summerofgoto.com

|url-status= usurped

}}

  • {{cite book

|last= Siedersleben

|first= Johannes

|editor= Christophe Dony

|title= Advanced Topics in Exception Handling Techniques

|url= https://archive.org/details/advancedtopicsex00dony

|url-access= limited

|year= 2006

|publisher= Springer Science & Business Media

|isbn= 978-3-540-37443-5

|page= [https://archive.org/details/advancedtopicsex00dony/page/n285 277]

|chapter= Errors and Exceptions - Rights and Obligations

}}

  • {{cite book

|last= Spinellis

|first= Diomidis

|title= Code Reading: The Open Source Perspective

|url= https://books.google.com/books?id=8lYbNfsAVT4C&pg=PA43

|date= 27 May 2003

|publisher= Addison-Wesley Professional

|isbn= 978-0-672-33370-5

|pages= 43–44

}}

  • {{cite web

|author= Stack Overflow Questions

|url= https://stackoverflow.com/questions/10975722/why-continue-is-considered-as-a-c-violation-in-misra-c2004

|title= Why "continue" is considered as a C violation in MISRA C:2004?

|publisher= Stack Overflow

|date= 2012-06-11

|access-date= 2021-11-10

}}

  • {{cite book

|last= Steele

|first= Guy Lewis

|title= Proceedings of the 1977 annual conference on - ACM '77

|chapter= Debunking the "expensive procedure call" myth or, procedure call implementations considered harmful or, LAMBDA

|author-link= Guy L. Steele, Jr.

|date= January 1977

|pages= 153–162

|doi= 10.1145/800179.810196

|isbn= 9781450323086

|s2cid= 9807843

|doi-access= free

}}

  • {{cite web

|last= Stroustrup

|first= Bjarne

|author-link= Bjarne Stroustrup

|url= http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style

|title= Day 1 Keynote - Bjarne Stroustrup: C++11 Style | GoingNative 2012 | Channel 9

|publisher= Channel9.msdn.com

|date= 2012-01-09

|access-date= 2021-11-10

}}

  • {{cite web

|last= Summit

|first= Steve

|url= http://c-faq.com/style/stylewars.html

|title= comp.lang.c FAQ list · Question 17.10

|publisher= C-faq.com

|year= 1995

|access-date= 2021-11-10

}}

  • {{Cite web

|author= The Go Programming Language Specification

|url= https://golang.org/ref/spec#Fallthrough_statements

|title= The Go Programming Language Specification - the Go Programming Language

|date= 26 July 2021

}}

  • {{cite web

|last= Torvalds

|first= Linus

|author-link= Linus Torvalds

|url= https://www.kernel.org/doc/html/v4.10/process/coding-style.html#centralized-exiting-of-functions

|title= Linux Kernel Coding Style

|publisher= The Linux Kernel’s documentation

|year= 2016

|access-date= 2021-11-10

}}

  • {{cite web

|last= Van Tassel

|first= Dennie

|url= http://www.gavilan.edu/csis/languages/labels.html#_Toc76036283

|title= History of Labels in Programming Languages

|date= July 8, 2004

|access-date= 4 January 2011

}}

  • {{cite book

|last= Vine

|first= Michael A.

|title= C Programming for the Absolute Beginner

|year= 2007

|publisher= Cengage Learning

|isbn= 978-1-59863-634-5

}}

  • {{Cite web

|last= Wagner

|first= Bill

|title= goto statement - C# Reference

|url= https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto

|year= 2021

|access-date= 2021-11-09

|website= docs.microsoft.com

|language= en-us

}}

  • {{cite book

|last1= Watt

|first1= David Anthony

|last2= Findlay

|first2= William

|title= Programming language design concepts

|url= https://archive.org/details/programminglangu00watt_497

|url-access= limited

|year= 2004

|publisher= John Wiley & Sons

|isbn= 978-0-470-85320-7

|page= [https://archive.org/details/programminglangu00watt_497/page/n246 228]

}}

  • {{Cite web

|last= Wehr

|first= Jason

|title= Go to ( assigned )

|url= http://www.personal.psu.edu/jhm/f90/statements/goto_a.html

|year= 1997

|access-date= 2021-11-13

|website= www.personal.psu.edu/jhm/f90/201.html

|language= en-us

}}

  • {{cite book

|last1= Wilkes

|first1= Maurice V.

|author-link1= Maurice Wilkes

|last2= Wheeler

|first2= David J.

|author-link2= David Wheeler (computer scientist)

|last3= Gill

|first3= Stanley

|author-link3= Stanley Gill

|title= The Preparation of Programs for an Electronic Digital Computer

|publisher= Addison-Wesley

|year= 1951

|quote= (sometimes called WWG, after its authors' initials) was the first book on computer programming

}}

  • {{cite journal

|last= Williams

|first= Tom

|url= https://issuu.com/rtcgroup/docs/rtc1303

|title= Checking Rules for C: Assuring Reliability and Safety

|journal= RTC Magazine

|date= March 2013

|volume= 22

|issue= 3

|pages= 12–15

|access-date= 2021-11-10

}}

  • {{cite web

|author= z/OS 2.5.0 in IBM Documentation

|url= https://www.ibm.com/docs/en/zos/2.5.0?topic=js-goto-statement#gsals__computed_goto

|title= Computed goto statement (IBM extension)

|publisher= IBM

|year= 2021

|access-date= 2021-11-13

|quote= This document describes the syntax, semantics, and IBM z/OS XL C/C++ implementation of the C and C++ programming languages. For a general-purpose C or C++ standard reference, see cppreference.com.

}}

{{refend}}

Category:BASIC commands

Category:Control flow

Category:Edsger W. Dijkstra