Goto (command)

From Academic Kids

(Redirected from GOTO)
This page is about the programming command. For information about the Japanese city, see Goto, Nagasaki.

GOTO is a command found in many programming languages which instructs the computer to jump to another point in the computer program, specified by a label or line number. It is the fundamental operation which can be used for transfer of control from one part of a program to another, and most compilers will translate other flow control statements into GOTOs.

GOTO is found in FORTRAN, Algol, COBOL, SNOBOL, BASIC, Common Lisp, C, C++, D, Pascal, Perl and many other languages, particularly assembly languages. In the assembly languages, the GOTO command is usually called BRA (from "branch"), JMP or JUMP, and is often the only way of organizing program flow. However GOTO is not found in all programming languages. In certain languages, such as Java, "goto" is a reserved word. In the parody programming language INTERCAL, COME FROM is used instead.

Unlike a function call, a GOTO does not demand any preparation or restructuring of the code. As a result, it becomes very easy to produce inconsistent, incomplete and generally unmaintainable spaghetti code. Consequently, as structured programming became more prominent in the 1960s and 1970s, numerous computer scientists came to the conclusion that programs should always use the structured flow commands (loops, if-then statements, etc.) in place of GOTO. However, others believed that even though the use of GOTO is often bad practice, there are some tasks that cannot be straightforwardly accomplished in many programming languages without the use of GOTO statements, such as exception handling.

Another criticism of using GOTO is that it can easily lead to an infinite loop. For example, the following code in BASIC will output an endless list of "Hello, world!".

10 PRINT "Hello, world!"
20 GOTO 10

One famous criticism of GOTO is a letter by Edsger Dijkstra called Go To Statement Considered Harmful. 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). Donald Knuth's Structured Programming with goto Statements considers some of the places where GOTO may be the appropriate tool. Generally these are in situations where a particular programming structure is not available. In these cases, GOTO can generally be used to emulate the desired structure, since it is one of the fundamental building blocks of programming. Another solution to this problem is writing the desired control structure as macro as one can do in almost all Lisp dialects.

There have been several variations on the GOTO statement. In BASIC, the ON GOTO statement selects from a list of different places to jump. It can be thought of as a primitive "switch" statement. The computed GOTO found in Fortran and some BASICs causes a jump to a line number computed by an arithmetic expression. Computed GOTO is often avoided even by programmers who use GOTO, since it makes code even more difficult to read: to determine even the possible destinations of the jump requires evaluating the arithmetic expression that controls it.

See also

References

  • Dijkstra, Edsger (1968) Go To Statement Considered Harmful, Communications of the ACM 11(3):147-148
  • Knuth, Donald (1974) Structured Programming with Goto Statements, Computing Surveys 6(4):261-301

External links

de:Goto he:Goto pl:Goto

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools