Skip to content
Home

goto (programming statement)

A programming statement that transfers control unconditionally to a labeled location; historically common but controversial due to maintainability and structured-program alternatives.

Overview

The goto statement is an instruction in many programming languages that causes an immediate jump to another place in the program identified by a label. In the simplest form it is an unconditional transfer of control: execution continues at the destination label instead of the next sequential statement. The keyword itself is derived from the English words "go" and "to" and appears in languages that expose low-level control flow primitives.

Image gallery

1 Image

How it works and common forms

Basic syntax typically looks like: goto label; followed later by the destination label: label:. Implementations vary: some languages permit only intra-function jumps, some allow jumps between blocks, and others (or their compilers) offer computed or indirect variants that branch to an address computed at runtime. For related concepts, see a general definition and lists of languages that historically supported goto.

History and debate

Goto was common in early high-level languages because machine code and assembly use jumps as the primitive control mechanism. From the 1960s onward, structured programming advocates argued that named constructs (loops, conditionals, subroutines) improve clarity. The most cited intervention is Edsger Dijkstra's 1968 essay arguing against indiscriminate use of goto; the mathematical result often cited alongside that essay is the structured program theorem (also called the Böhm–Jacopini result) showing that sequenced composition, selection, and iteration suffice to express any computable function. Still, some languages preserve goto for compatibility or low-level control. For example, Java has the word reserved but disallows its use, while languages like C, Pascal and many assemblers provide it directly. The notion of reserved words explains why some keywords exist without being usable in the same way across languages.

Uses, examples and practical considerations

Programmers use goto sparingly for reasons such as implementing state machines, performing early exits from deeply nested loops, or writing compact error-handling cleanup in languages without structured exceptions (a common idiom in C). Example pattern in C-style pseudocode: if (error) goto cleanup; followed by a single cleanup label. Many modern alternatives — multi-level breaks, labeled breaks, exceptions, or decomposing code into functions — make most uses unnecessary. The role of subroutines and modularization is explained in sources about functions and methods.

Advantages and disadvantages

  • Advantages: direct mapping to machine jumps, sometimes clearer or more efficient for tiny, low-level tasks, and useful in some resource-cleanup idioms.
  • Disadvantages: excessive use can produce "spaghetti code" that is hard to follow, reason about, and maintain; it can also undermine local reasoning, testing, and refactoring.

Notable distinctions and modern practice

Different languages take different stances. Some restrict goto to avoid entering scopes with initialized variables; others provide labeled statements and structured exits that mimic goto behavior safely. Compilers and tools often transform structured constructs into jump-based machine code, so every high-level construct ultimately relies on jumps at low level. Contemporary style guides typically recommend avoiding goto except in narrowly justified situations; understanding both the theory and pragmatic trade-offs helps developers decide when a simple jump is the clearest solution.

For further reading on the concept, historical debates, and idioms around transfer-of-control statements, consult general language references and historical essays on structured programming and control-flow design.

Definition | Languages | Java reserved example | Reserved words | Structured program theorem | Functions and methods

Questions and answers

Q: What is a goto statement?

A: A goto statement is a way to jump to another line of code in many programming languages.

Q: How many programming languages support the goto statement?

A: Many programming languages support the goto statement.

Q: In which programming language is goto a reserved word?

A: In Java, goto is a reserved word, but cannot be used.

Q: What is a reserved word in a programming language?

A: A reserved word is a word that is a part of the programming language and cannot be used for other things like naming variables.

Q: What is the structured program theorem in Computer Science?

A: The structured program theorem is a theory in Computer Science that says any program can be written in such a way that things are done with functions and methods instead of in one very big program and goto statements.

Q: Does the structured program theorem prove that goto statements are needed to write programs?

A: No, the structured program theorem proves that the goto statement is not needed to write programs.

Q: What are functions and methods in programming?

A: Functions and methods are smaller sub-programs used in programming to perform specific tasks.

Related articles

Author

AlegsaOnline.com goto (programming statement)

URL: https://en.alegsaonline.com/art/39882

Share

Sources