Goto Statement
The goto
statement is used for unconditional jump to a local label — for more information on labels, refer to Labeled Statements. The syntax of the goto
statement is:
goto label_identifier;
This will transfer control to the location of a local label specified by label_identifier
. The label_identifier
has to be a name of the label within the same function in which the goto
statement is. The goto
line can come before or after the label.
goto
is used to break out from any level of nested control structures but it cannot be used to jump into block while skipping that block’s initializations – for example, jumping into loop’s body, etc.
The use of goto
statement is generally discouraged as practically every algorithm can be realized without it, resulting in legible structured programs. One possible application of the goto
statement is breaking out from deeply nested control structures:
for (...) { for (...) { ... if (disaster) goto Error; ... } } . . . Error: /* error handling code */
What do you think about this topic ? Send us feedback!