goto

statement invoking one-way transfer of program control in many languages

goto is a statement in many programming languages. It is a combination of the English words go and to. It is a way to jump to another line of code.

Many languages support the goto statement, and many do not. In Java, goto is a reserved word, but cannot be used.[1][2] (A reserved word is a word that is a part of the programming language and cannot be used for other things like naming variables.)

In Computer Science, there is a theory called the structured program theorem. This theory says that any program can be written in such a way that things are done with functions and methods (smaller sub-programs) instead of in one very big program and goto statements. The theory proves that the goto statement is not needed to write programs.

A goto statement is usually used with a label. A label is a word that identifies a place in a piece of code. A goto statement in code usually looks like this:

goto LABEL

In this example, the computer would jump to the location of LABEL.

Many times the goto statement is, combined with the if statement. In this case, the computer will only jump to another place in the code if something is true. For example:

IF condition THEN goto LABEL

This can be read as "if some condition is true, then go to the location of LABEL."

Different programming languages have different restrictions or limits on the use of goto statements. For example, the C programming language does not allow a goto to jump to a location inside of another function (subprogram).

References

change
  1. "The Java Language Specification, Third Edition". 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". 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.