C++ goto
statement
The goto
statement in C++ is a control flow statement that allows the program to jump to a different part of the program. Although it can be useful in certain situations, the goto
statement is generally considered poor practice because it can make your code difficult to understand and maintain. Instead, it is often recommended to use structured control flow statements (such as loops and conditionals) wherever possible.
However, there are cases where the goto
statement might be useful, particularly when you need to break out of deeply nested loops or handle error conditions.
In this blog post, we'll cover:
goto
statement.goto
with labels in C++.goto
should be used sparingly.goto
StatementThe basic syntax of the goto
statement is:
goto label;
Here, label
is an identifier that refers to a specific point in the program. When the goto
statement is executed, control is transferred to the statement immediately following the label.
label:
// Code to execute when control jumps here
The label is defined by placing an identifier followed by a colon (:
). This label can be anywhere in the code, but it should be placed before the code you want to jump to.
goto
StatementThe goto
statement can be used to transfer control within a function, including jumping backward or forward. Let's look at a basic example of how it works.
goto
Usage
#include <iostream>
using namespace std;
int main() {
int i = 0;
// Label to jump to
start_loop:
if (i == 5) {
cout << "Reached the target: " << i << endl;
goto end; // Jump to 'end' label
}
cout << "i = " << i << endl;
i++;
goto start_loop; // Jump back to 'start_loop'
end:
cout << "Loop finished." << endl;
return 0;
}
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Reached the target: 5
Loop finished.
Explanation:
start_loop
label, and the loop continues until i
reaches 5.i
equals 5, the goto end
statement transfers control to the end
label, skipping the rest of the loop and printing "Loop finished."goto
to Exit Nested LoopsOne of the common use cases for the goto
statement is when you need to exit from deeply nested loops. Without goto
, you might need to use flags or additional logic to break out of multiple levels of loops. Here's an example:
goto
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 3 && j == 3) {
cout << "Breaking out of nested loops at i = " << i << ", j = " << j << endl;
goto end_loop; // Jump out of both loops
}
cout << "i = " << i << ", j = " << j << endl;
}
}
end_loop:
cout << "Exited from the nested loops." << endl;
return 0;
}
Output:
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 2, j = 4
i = 3, j = 0
i = 3, j = 1
i = 3, j = 2
Breaking out of nested loops at i = 3, j = 3
Exited from the nested loops.
Explanation:
i
equals 3 and j
equals 3, the goto end_loop
statement is executed.end_loop
label, exiting both loops immediately.goto
in Error HandlingAnother scenario where goto
can be useful is in error handling. You might want to exit a function or program when an error condition is met, and the goto
statement allows you to jump directly to an error-handling section of your code.
goto
#include <iostream>
using namespace std;
int main() {
int value;
// Simulating an error scenario
cout << "Enter a positive number: ";
cin >> value;
if (value <= 0) {
cout << "Invalid input! Exiting program." << endl;
goto error; // Jump to error handling
}
cout << "You entered: " << value << endl;
return 0;
error:
cout << "Error occurred. Exiting safely..." << endl;
return 1;
}
Output (with invalid input):
Enter a positive number: -5
Invalid input! Exiting program.
Error occurred. Exiting safely...
Explanation:
error
label and prints an error message.goto
is Generally DiscouragedWhile the goto
statement can be useful in certain situations, it is generally discouraged because it can make your code harder to understand and maintain. Some of the issues with goto
include:
goto
can make it harder to follow the program's flow, as it jumps around unpredictably.goto
heavily, it can become challenging to modify the program, especially when dealing with large codebases.In most cases, you can achieve the same results with more structured control flow mechanisms like loops and conditionals.
goto
break
or continue
statements in conjunction with flags or nested loops.goto
.