The goto statement has the form:
goto label;
where 'label' is an identifier. This identifier, followed by a colon, ':', appears before a statement somewhere in the same function as the "goto" statement. See: 'Labelled Statements' in this menu.
A goto statement unconditionally transfers control to the statement which follows the label. The labelled statement may be a null statement, i.e. it consists only of a semicolon, ';'. The labelled statement may be before or after the goto statement.
label: statement_1; /* label is before the goto */ ... statement_2; goto label;
A backwards jump or goto creates what is called a "loop", as is clearly seen in the flow chart (below), provided that there is a control flow path in the function between: statement_1 and statement_2.
A 'loop' is a very important program structure. It is the basis of iteration. (Iteration is a loop that repeats some calculation or algorithm for a set of different values selected by the loop index or some other variable which changes value.)
A label before a statement must be separated from that statement by white space, hence it can be on the same line or a different line in the source file.
The flow chart for the above example of a goto which jumps backwards is:
| v | +-<----------\ label:| | +-------------+ | | statement_1 | | | ... | | | statement_2 | | +-------------+ | | | v | \------------/
Here is program fragment where a goto is used to jump forward:
statement_1; ... statement_2; goto label; ... label: statement_n1; ... statement_n2;
By itself, this control flow structure does not produce a loop in the program, or more precisely, in a function. The flow chart for the above follows.
| v +-------------+ | statement_1 | | ... | | statement_2 | +-------------+ | \------------\ | v label: +--------------+ | statement_n1 | | ... | | statement_n2 | +--------------+ | v
If the statement immediately after a "goto" statement (forward or backward) does not have a label, then there is no way to reach it. Most compilers will give a warning about this and delete from the program all such 'unreachable' code.
Now that we have said so much and spent so much time on the goto statement, we must say that it is the least desirable control flow statement to use! Why is this? Over the years, as larger and larger programs were written, it was found that using goto's to get from one place to another within a function would tangle the flow into what is called 'spaghetti code'. A goto would be used to jump to some code fragment shared after different sequences of statements. Later this shared code, this list of statements at the label, would would be modified. Then, some of the places jumping there would still work, while others might not because they needed the unmodified code as the target of the goto.
As the goto's proliferated in a function and as that function grew in size, it became more and more difficult for the programmer to visualize the flow that was taking place in the program, and to see the consequences of a single change to the program.
If there was a loop, it became hard to see it in the source file. A flow chart would help, but it also became difficult to keep straight and up to date. As people had more and more experience writing, modifying and fixing programs, it became clear that the goto statement was part of the problem. To solve this problem, the other control flow statements were invented. The solution, is then to use the other control flow statements where ever possible, as long as that did not make the program too awkward.
This is not to say that, as some would have wished, the goto can be completely eliminated. There are some control flow structures that cannot be written without a goto.