Many languages support the goto statement, and many do not. In Java, goto is a reserved word, but cannot be used. (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.
Use[change | change source]
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:
In this example, the computer would jump to the location of LABEL.
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 | change source]
- "The Java Language Specification, Third Edition". http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.9. "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."
- "The Java Language Specification, Third Edition". http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.7. "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."