The goto statement transfers control to the location specified by label.The goto statement must be in the same function as the label it is referring, it may appear before or after the label. If transfer of control exits the scope of any automatic variables (e.g. By jumping backwards to a point before the declarations of such variables or by jumping forward out of a.
- C++ Basics
- C++ Object Oriented
- C++ Advanced
- C++ Useful Resources
- Selected Reading
A goto statement provides an unconditional jump from the goto to a labeled statement in the same function.
NOTE − Use of goto 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 that uses a goto can be rewritten so that it doesn't need the goto.
Syntax
The syntax of a goto statement in C++ is −
Where label is an identifier that identifies a labeled statement. A labeled statement is any statement that is preceded by an identifier followed by a colon (:).
Flow Diagram
Example
When the above code is compiled and executed, it produces the following result −
One good use of goto is to exit from a deeply nested routine. For example, consider the following code fragment −
Eliminating the goto would force a number of additional tests to be performed. A simple break statement would not work here, because it would only cause the program to exit from the innermost loop.
Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
Labels | ||||
label : statement | ||||
Expression statements | ||||
expression ; | ||||
Compound statements | ||||
{ statement... } | ||||
Selection statements | ||||
if | ||||
switch | ||||
Iteration statements | ||||
while | ||||
do-while | ||||
for | ||||
range for(C++11) | ||||
Jump statements | ||||
break | ||||
continue | ||||
return | ||||
goto | ||||
Declaration statements | ||||
declaration ; | ||||
Try blocks | ||||
try compound-statementhandler-sequence | ||||
Transactional memory | ||||
synchronized , atomic_commit , etc(TM TS) |
Transfers control unconditionally.
Used when it is otherwise impossible to transfer control to the desired location using other statements.
[edit]Syntax
attr(optional)goto label; |
[edit]Explanation
The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.
If transfer of control exits the scope of any automatic variables (e.g. by jumping backwards to a point before the declarations of such variables or by jumping forward out of a compound statement where the variables are scoped), the destructors are called for all variables whose scope was exited, in the order opposite to the order of their construction.
The goto
statement cannot transfer control into a try-block or into a catch-clause, but can transfer control out of a try-block or a catch-clause (the rules above regarding automatic variables in scope are followed)
If transfer of control enters the scope of any automatic variables (e.g. by jumping forward over a declaration statement), the program is ill-formed (cannot be compiled), unless all variables whose scope is entered have
C++ Goto Line
(Note: the same rules apply to all forms of transfer of control)
[edit]Keywords
[edit]Notes
In the C programming language, the goto
statement has fewer restrictions and can enter the scope of any variable other than variable-length array or variably-modified pointer.
Goto Function In Dev C Example
[edit]Example
Goto Function In Excel
Output:
[edit]Further Reading
The popular Edsger W. Dijkstra essay, “Goto Considered Harmful”, presents a survey of the many subtle problems the careless use of this keyword can introduce.
[edit]See Also
Goto Function In Dev C Online
C documentation for goto |