C++ goto Keyword
The goto keyword in C++ provides an unconditional jump to a labeled statement in the program. It is primarily used to alter the flow of control and jump to a specific part of the program. The goto keyword can make code less readable and harder to debug, so it should be used cautiously and only when necessary.
The use of goto is discouraged in modern programming due to the potential for creating “spaghetti code,” where the program’s control flow becomes tangled and difficult to follow. Structured alternatives like loops and functions are usually preferred.
Syntax
</>
                        Copy
                        goto label;
// Code block
label:
// Code to execute after the jump
- goto
- The keyword used to jump to a specific label in the program.
- label
- An identifier marking the location in the code to which control jumps. It must end with a colon (:).
Examples
Example 1: Using goto for Early Exit
In this example, we will learn how to use goto to break out of nested loops.
</>
                        Copy
                        #include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (i == 2 && j == 2) {
                goto exit_loop; // Jump to the label
            }
            cout << "i: " << i << ", j: " << j << endl;
        }
    }
exit_loop:
    cout << "Exited nested loops." << endl;
    return 0;
}
Output:
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
Exited nested loops.Explanation:
- The outer loop iterates over i, and the inner loop iterates overj.
- When i == 2andj == 2, thegotostatement is executed, jumping to theexit_looplabel.
- The program skips the remaining iterations of both loops and executes the code after the label.
Example 2: Error Handling with goto
In this example, we will learn how to use goto for handling an error scenario.
</>
                        Copy
                        #include <iostream>
using namespace std;
int main() {
    int numerator = 10, denominator = 0;
    if (denominator == 0) {
        goto error; // Jump to error handling code
    }
    cout << "Result: " << numerator / denominator << endl;
    return 0;
error:
    cout << "Error: Division by zero." << endl;
    return 1;
}
Output:
Error: Division by zero.Explanation:
- The variables numeratoranddenominatorare initialized.
- Before performing division, the program checks if denominator == 0.
- If the condition is true, the gotostatement jumps to theerrorlabel.
- The error message is printed, and the program exits with a non-zero return code indicating an error.
Key Points about goto Keyword
- The gotokeyword allows unconditional jumps to a labeled statement in the program.
- It can simplify breaking out of deeply nested loops or handling errors in certain scenarios.
- Using gotocan make code harder to understand and debug, so structured alternatives like loops and functions are usually preferred.
- Labels must be unique within their scope and followed by a colon (:).
- Use gotojudiciously and only when other control flow mechanisms are not feasible.
