How to Utilize the goto Statement in C++? Last Updated : 28 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The goto statement in C++ is a control flow statement that allows the users to move the control flow from one part to another part of the program. In this article, we will learn how to utilize the goto statement in C++. Utilize the goto Statement in C++In C++, the goto statement transfers the program's control flow from one part to another. It allows the users to jump to another part of the program while skipping the current part. Although it is generally advised to use structured control flow statements for readability and maintainability, there are many scenarios where the goto statement can be useful. Following is the syntax for goto: Syntax#include <iostream>int main() { // ... code before goto goto label; // Jump to the label // ... code that will be skipped by the gotolabel: // ... code after the label return 0;}C++ Program to Utilize the goto StatementThe following program demonstrates how we can utilize the goto statement to break out of multiple nested loops C++ // C++ Program to Utilize the goto Statement #include <iostream> using namespace std; int main() { for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (i == 1 && j == 2) { // Exit both loops goto endLoops; } cout << i << ", " << j << endl; } } endLoops: cout << "Exited the nested loops." << endl; return 0; } Output0, 0 0, 1 0, 2 1, 0 1, 1 Exited the nested loops. Time Complexity: O(1), as goto is used to terminate the loop iterationsAuxiliary Space: O(1) Utilize goto for error handlingThe following program illustrates how goto statement can be used for error handling in order to avoid unnecessary calculations: C++ // C++ Program to utilize goto for error handling #include <iostream> #include <limits> using namespace std; int main() { double numerator = 10.0; double denominator = 0.0; double result; if (denominator == 0) { // Jump to cleanup if denominator is 0 goto cleanup; } // Perform the division result = numerator / denominator; cout << "Result: " << result << endl; cleanup: cerr << "Error: Division by zero is not allowed." << endl; cout << "Program has finished executing." << endl; return 0; } Output ERROR!Error: Division by zero is not allowed.Program has finished executing.Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Take Multiple Input from User in C++? K kumardhanshiv Follow Improve Article Tags : C++ Programs C++ C-Loops & Control Structure CPP-Control-Flow CPP Examples misc-cpp +2 More Practice Tags : CPPCPP-Control-Flow Similar Reads How to Read Input Until EOF in C++? In C++, EOF stands for End Of File, and reading till EOF (end of file) means reading input until it reaches the end i.e. end of file. In this article, we will discuss how to read the input till the EOF in C++. Read File Till EOF in C++The getline() function can be used to read a line in C++. We can 2 min read How to Use the ignore() Function in C++? In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio 2 min read How to Use the Not-Equal (!=) Operator in C++? The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ 3 min read How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp 2 min read How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 2 min read How to Add Multiple Conditions for if Statement in C++? In C++, an if statement is used to decide whether a specific block of code should execute, depending on a given condition. It usually evaluate a single expression to check condition. In this article, we will learn how to add multiple conditions as a single expression for an if statement in C++. Mult 2 min read Like