9/14/2020 C++ goto statement - Tutorialspoint
https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 1/3
C++ goto statementC++ goto statement
AA gotogoto statement provides an unconditional jump from the goto to a labeled statement in the samestatement provides an unconditional jump from the goto to a labeled statement in the same
function.function.
NOTENOTE − Use of− Use of gotogoto statement is highly discouraged because it makes difficult to trace the controlstatement is highly discouraged because it makes difficult to trace the control
flow of a program, making the program hard to understand and hard to modify. Any program thatflow of a program, making the program hard to understand and hard to modify. Any program that
uses a goto can be rewritten so that it doesn't need the goto.uses a goto can be rewritten so that it doesn't need the goto.
SyntaxSyntax
The syntax of a goto statement in C++ is −The syntax of a goto statement in C++ is −
goto label;goto label;
....
..
label: statement;label: statement;
WhereWhere labellabel is an identifier that identifies a labeled statement. A labeled statement is anyis an identifier that identifies a labeled statement. A labeled statement is any
statement that is preceded by an identifier followed by a colon (:).statement that is preceded by an identifier followed by a colon (:).
Flow DiagramFlow Diagram
ExampleExample
9/14/2020 C++ goto statement - Tutorialspoint
https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 2/3
#include#include <iostream><iostream>
usingusing namespacenamespace stdstd;;
intint mainmain ()() {{
// Local variable declaration:// Local variable declaration:
intint aa == 1010;;
// do loop execution// do loop execution
LOOPLOOP::dodo {{
ifif(( aa ==== 1515)) {{
// skip the iteration.// skip the iteration.
aa == aa ++ 11;;
gotogoto LOOPLOOP;;
}}
coutcout <<<< "value of a: ""value of a: " <<<< aa <<<< endlendl;;
aa == aa ++ 11;;
}}
whilewhile(( aa << 2020 ););
returnreturn 00;;
}}
When the above code is compiled and executed, it produces the following result −When the above code is compiled and executed, it produces the following result −
value of a: 10value of a: 10
value of a: 11value of a: 11
value of a: 12value of a: 12
value of a: 13value of a: 13
value of a: 14value of a: 14
value of a: 16value of a: 16
value of a: 17value of a: 17
value of a: 18value of a: 18
value of a: 19value of a: 19
One good use of goto is to exit from a deeply nested routine. For example, consider the followingOne good use of goto is to exit from a deeply nested routine. For example, consider the following
code fragment −code fragment −
for(...) {for(...) {
for(...) {for(...) {
while(...) {while(...) {
if(...) goto stop;if(...) goto stop;
..
..
..
Live DemoLive Demo
9/14/2020 C++ goto statement - Tutorialspoint
https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 3/3
}}
}}
}}
stop:stop:
cout << "Error in program.n";cout << "Error in program.n";
Eliminating theEliminating the gotogoto would force a number of additional tests to be performed.would force a number of additional tests to be performed. A simpleA simple breakbreak
statement would not work here, because it would only cause the program to exit from the innermoststatement would not work here, because it would only cause the program to exit from the innermost
loop.loop.

C++ goto statement tutorialspoint

  • 1.
    9/14/2020 C++ gotostatement - Tutorialspoint https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 1/3 C++ goto statementC++ goto statement AA gotogoto statement provides an unconditional jump from the goto to a labeled statement in the samestatement provides an unconditional jump from the goto to a labeled statement in the same function.function. NOTENOTE − Use of− Use of gotogoto statement is highly discouraged because it makes difficult to trace the controlstatement is highly discouraged because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program thatflow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto.uses a goto can be rewritten so that it doesn't need the goto. SyntaxSyntax The syntax of a goto statement in C++ is −The syntax of a goto statement in C++ is − goto label;goto label; .... .. label: statement;label: statement; WhereWhere labellabel is an identifier that identifies a labeled statement. A labeled statement is anyis an identifier that identifies a labeled statement. A labeled statement is any statement that is preceded by an identifier followed by a colon (:).statement that is preceded by an identifier followed by a colon (:). Flow DiagramFlow Diagram ExampleExample
  • 2.
    9/14/2020 C++ gotostatement - Tutorialspoint https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 2/3 #include#include <iostream><iostream> usingusing namespacenamespace stdstd;; intint mainmain ()() {{ // Local variable declaration:// Local variable declaration: intint aa == 1010;; // do loop execution// do loop execution LOOPLOOP::dodo {{ ifif(( aa ==== 1515)) {{ // skip the iteration.// skip the iteration. aa == aa ++ 11;; gotogoto LOOPLOOP;; }} coutcout <<<< "value of a: ""value of a: " <<<< aa <<<< endlendl;; aa == aa ++ 11;; }} whilewhile(( aa << 2020 );); returnreturn 00;; }} When the above code is compiled and executed, it produces the following result −When the above code is compiled and executed, it produces the following result − value of a: 10value of a: 10 value of a: 11value of a: 11 value of a: 12value of a: 12 value of a: 13value of a: 13 value of a: 14value of a: 14 value of a: 16value of a: 16 value of a: 17value of a: 17 value of a: 18value of a: 18 value of a: 19value of a: 19 One good use of goto is to exit from a deeply nested routine. For example, consider the followingOne good use of goto is to exit from a deeply nested routine. For example, consider the following code fragment −code fragment − for(...) {for(...) { for(...) {for(...) { while(...) {while(...) { if(...) goto stop;if(...) goto stop; .. .. .. Live DemoLive Demo
  • 3.
    9/14/2020 C++ gotostatement - Tutorialspoint https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm#:~:text=A goto statement provides an,understand and hard to modify. 3/3 }} }} }} stop:stop: cout << "Error in program.n";cout << "Error in program.n"; Eliminating theEliminating the gotogoto would force a number of additional tests to be performed.would force a number of additional tests to be performed. A simpleA simple breakbreak statement would not work here, because it would only cause the program to exit from the innermoststatement would not work here, because it would only cause the program to exit from the innermost loop.loop.