All Topics  
GOTO

 

   Email Print
   Bookmark   Link






 

GOTO



 
 
GOTO is a statement
Statement (programming)

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements....
 found in many computer programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s. It is a combination of the English words go and to. When executed it causes an unconditional transfer of control
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
 (a "jump") to another statement. The jumped-to statement is specified using some kind of label
Label (programming language)

A label in a programming language is a sequence of characters that identifies a location within source code. In most languages labels take the form of an identifier, often followed by a punctuation ....
, which may be an identifier
Identifier

In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems....
 or a line number
Line number

In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line....
 depending on the language. At the machine code
Machine code

Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit. Machine code may be regarded as a primitive programming language or as the lowest-level representation of a compiled and/or assembly language computer program....
 level a goto is a form of branch or jump statement
Branch (computer science)

A branch is a point in a computer program where the flow of control is altered. The term branch is usually used when referring to a program written in machine code or assembly language; in a high-level programming language, branches usually take the form of conditional statements, subroutine calls or GOTO statements....
.

In some languages, goto functionality may be present without explicit use of the keyword
Keyword (computer programming)

In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords ? and, indeed, the meaning of the notion of keyword ? differs widely from language to language....
 goto, such as where a break or continue keyword may be followed by an identifier denoting a label.






Discussion
Ask a question about 'GOTO'
Start a new discussion about 'GOTO'
Answer questions from other users
Full Discussion Forum



Encyclopedia


GOTO is a statement
Statement (programming)

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements....
 found in many computer programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s. It is a combination of the English words go and to. When executed it causes an unconditional transfer of control
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
 (a "jump") to another statement. The jumped-to statement is specified using some kind of label
Label (programming language)

A label in a programming language is a sequence of characters that identifies a location within source code. In most languages labels take the form of an identifier, often followed by a punctuation ....
, which may be an identifier
Identifier

In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems....
 or a line number
Line number

In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line....
 depending on the language. At the machine code
Machine code

Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit. Machine code may be regarded as a primitive programming language or as the lowest-level representation of a compiled and/or assembly language computer program....
 level a goto is a form of branch or jump statement
Branch (computer science)

A branch is a point in a computer program where the flow of control is altered. The term branch is usually used when referring to a program written in machine code or assembly language; in a high-level programming language, branches usually take the form of conditional statements, subroutine calls or GOTO statements....
.

In some languages, goto functionality may be present without explicit use of the keyword
Keyword (computer programming)

In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords ? and, indeed, the meaning of the notion of keyword ? differs widely from language to language....
 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.

While GOTO statements are found in most high-level languages, there are a few high-level languages that do not support them. For instance, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 (where goto is a reserved word
Reserved word

Reserved words are one type of grammatical construct in programming languages. These words have special meaning within the language and are predefined in the language?s formal specifications....
 but does not presently serve any function).

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. The 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
Spaghetti code

Spaghetti code is a pejorative term for source code which has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" Branch constructs....
". As structured programming
Structured programming

Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO Statement ....
 became more popular in the 1960s and 1970s, many computer scientist
Computer scientist

A computer scientist is a person who has acquired knowledge of computer science, the study of the theoretical foundations of information and computation and their application in computer systems....
s 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. Even today some programming style
Programming style

Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors....
 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
Finite state machine

A finite state machine or finite state automaton or simply a state machine, is a model of behavior composed of a finite number of state s, transitions between those states, and actions....
s, breaking out of nested loops and exception handling
Exception handling

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution....
.

Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra
Edsger Dijkstra

Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
 called Go To Statement Considered Harmful
Considered harmful

In computer science and related disciplines, considered harmful is a phrase popularly used in the titles of diatribes and other critical essays ....
. 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
Donald Knuth

Donald Ervin Knuth is a renowned computer science and Emeritus of the Art of Computer Programming at Stanford University.Author of the seminal multi-volume work The Art of Computer Programming , Knuth has been called the "father" of the run-time analysis, contributing to the development of, and systematizing formal mathematical techn...
'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.

This criticism had an effect on the design of some programming languages. Although the designers of the Ada
Ada (programming language)

Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
 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. 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. <>) 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;.

Variations


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

Restricted GOTOs

Many languages, such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 and Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' 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
Iteration

Iteration means the act of repeating....
 block - either to continue a loop at the next iteration (continue), or to end the loop (break).

switch/case structures

The switch statement
Switch statement

In computer programming, a switch statement is a type of control Statement that exists in most modern imperative programming languages . Its purpose is to allow the value of a variable or expression to control the flow of program execution....
 in C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, C++ and Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' 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 GOTO (originally Fortran
Fortran

Fortran is a general-purpose programming language, procedural programming language, imperative programming language programming language that is especially suited to numerical analysis and scientific computing....
 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
BASIC

In computer programming, BASIC is a family of high-level programming languages. The Dartmouth BASIC was designed in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College in New Hampshire, United States to provide computer access to non-science students....
 supports the first kind of computed GOTO and is useful for case-by-case branching, as in C's switch statement
Switch statement

In computer programming, a switch statement is a type of control Statement that exists in most modern imperative programming languages . Its purpose is to allow the value of a variable or expression to control the flow of program execution....
. Some C compilers (e.g., gcc
GNU Compiler Collection

The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain....
) 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.

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).

Continuations

A continuation
Continuation

In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
 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
Call stack

In computer science, a call stack is a dynamic Stack data structure that stores information about the active subroutines of a computer program....
 in addition to a jump. The longjmp function of the C programming language
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 is an example of an escape continuation that may be used to escape the current context to a surrounding one. The Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 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
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
.

"COME FROM" parody

In the esoteric programming language
Esoteric programming language

An esoteric programming language is a programming language designed as a test of the boundaries of computer programming language design, as a proof of concept, or as a joke....
 INTERCAL, which is a parody
Parody

A parody , in contemporary usage, is a work created to mock, comment on, or poke fun at an original work, its subject, or author, or some other target, by means of humorous, satiric or ironic imitation....
 of languages like BASIC, COME FROM
COMEFROM

In computer programming, COMEFROM is an obscure control flow structure used in some programming languages, primarily as a joke.COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement....
 is used instead of GOTO.

Perl GOTO

In Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, 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
Tail call

A tail call is a call to a subroutine which is followed by a return to the calling code. Such a call is said to be in tail position. If a subroutine performs a tail call to itself, it is called tail recursion....
): 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
COBOL

COBOL is one of the oldest programming languages still in active use. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
 had the ALTER verb to accomplish this.

See also

  • Unstructured programming
  • Control flow
    Control flow

    In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
  • GOSUB
    GOSUB

    GOSUB is a command in many versions of BASIC programming language. GOSUB statements branch to simple kinds of Subroutine without parameters or local variable , the Return statement command resuming program flow from the point at which GOSUB was invoked....


External links

  • . Summarized arguments agains and pro usage of goto