ArticleslgStudy

science

Goto

Goto is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Goto rather than just read about it. In short: In computer programming, goto is a control flow statement that transfers control to another line of source code. Unlike a function call that supports returning to the point of call, goto does not.

Goto — main illustration
Goto — illustration

Key takeaways

  • Goto belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Goto to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Goto from memory before moving on to harder problems.

Reference excerpt

In computer programming, goto is a control flow statement that transfers control to another line of source code. Unlike a function call that supports returning to the point of call, goto does not. The statement is denoted differently by programming language; some use lowercase (goto), some use uppercase (GOTO), and others are case-insensitive. A few languages make the statement two words (i.e. GO TO). A goto statement is included in a language, primarily, to provide access to the machine code jump instruction (a.k.a. branch or transfer), but due to potential problems with the use of jump semantics, languages have over time been augmented with other flow-control mechanisms intended to replace the need for and use of goto. Many modern languages do not include a goto statement at all. Many languages that do include a goto statement restrict its use in order to limit the problems that its use might incur. Further, as its use is generally considered a poor choice, software developers tend to avoid using it even when using a language that provides it. In general, use of goto is considered a poor choice as it leads to code with higher cognitive load and more bugs than code that uses more structured flow-control. The use of goto was common in the early days of computing, but via the concerted effort of the structured programming movement in the 1960s and 1970s, that aimed to replace goto with more structured flow-control, its use has declined significantly. Nonetheless, goto is still used today, but generally limited to specific scenarios. 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, and iteration are sufficient for any computation that can be performed by a Turing machine, with the caveat that code duplication and additional variables may be required.

Language support Support of goto varies by language. Languages designed long ago tend to have unrestricted support and newer languages tend to have restricted or no support. For example, C does not permit a jump to a label in a different function. C# and Visual Basic .NET both support goto. However, they do not allow jumping to a label outside of the containing scope, and respects object disposal and finally constructs, making it significantly less powerful and less dangerous than goto in other languages. They also make case and default keywords labels, whose scope is the enclosing switch statement. goto case or goto default is used as a replacement for implicit fallthrough, which is not allowed. Many languages lack goto semantics. Java, has a reserved word goto but it is not usable, although compiled .class files generate goto and label statements. Python does not support goto, although there are several joke modules that provide it. PHP did not support goto until version 5.3. PL/I has a goto statement that unwinds the stack for an out-of-block transfer and does not permit a transfer into a block. Most languages that have a goto-semantics statement use the keyword goto, but other syntax is used; particularly in older languages. For example, MAD uses TRANSFER TO, and APL uses a right-pointing arrow, →. Functional programming languages such as Scheme generally do not have goto, instead using continuations.

Computed and assigned

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. The equivalent construct in C is the switch statement, and in newer Fortran a SELECT CASE construct is the recommended syntactical alternative. BASIC had a 'On GoTo' statement that achieved the same goal, but in Visual Basic this construct is no longer supported. 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 goto statements. 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:

Several C compilers implement two non-standard C/C++ extensions relating to goto statements originally introduced by gcc. 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. 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. 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). PL/I label variables achieve the effect of the computed or assigned goto.

ALTER Up to the 1985, ANSI standard COBOL had the ALTER statement which could be used to change the destination of a 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.

Perl goto Perl has a goto statement that takes a function name and transfers control by effectively substituting one function call for another (a tail call). The new function does not return to the goto, but instead to the place from which the original function was called.

… excerpt ends here. Continue reading the full article.

Illustrations

Goto: "GOTO" key on the 1982 ZX Spectrum home computer, implemented with native BASIC (one-key command entry).
"GOTO" key on the 1982 ZX Spectrum home computer, implemented with native BASIC (one-key command entry).

Worked examples

Example 1 — a first encounter with Goto

Start with the simplest possible case. Write down what Goto claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Goto before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Goto ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Goto

In research
Goto appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Goto in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Goto is common in secondary-school and first-year university syllabi. It links to neighbouring topics BASIC commands, Control flow, Edsger W. Dijkstra, so understanding it makes those chapters shorter.
In everyday life
Look for Goto outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Goto” →

How to study Goto in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Goto means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Goto out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Goto in simple terms?

In computer programming, goto is a control flow statement that transfers control to another line of source code. Unlike a function call that supports returning to the point of call, goto does not.

Why does Goto matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Goto?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Goto.

Tags

  • BASIC commands
  • Control flow
  • Edsger W. Dijkstra

Keep exploring