C++ goto statement tutorialspoint

NAMITHNAVAKRISHNAN 570 views 3 slides Sep 29, 2020
Slide 1
Slide 1 of 3
Slide 1
1
Slide 2
2
Slide 3
3

About This Presentation

c




















































































programming


Slide Content

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
A A gotogoto statement provides an unconditional jump from the goto to a labeled statement in the same statement 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 control statement 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;
Where Where labellabel is an identifier that identifies a labeled statement. A labeled statement is any is 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 std std;;

intint main main ()() {{
// Local variable declaration:// Local variable declaration:
intint a a == 1010;;

// do loop execution// do loop execution
LOOP LOOP::dodo {{
ifif(( a a ==== 1515)) {{
// skip the iteration.// skip the iteration.
a a == a a ++ 11;;
gotogoto LOOP LOOP;;
}}
cout cout <<<< "value of a: ""value of a: " <<<< a a <<<< endl endl;;
a a == a a ++ 11;;
}}
whilewhile(( a a << 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 the Eliminating the gotogoto would force a number of additional tests to be performed. would force a number of additional tests to be performed. A simple A 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.
Tags