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:

  1. The outer loop iterates over i, and the inner loop iterates over j.
  2. When i == 2 and j == 2, the goto statement is executed, jumping to the exit_loop label.
  3. 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:

  1. The variables numerator and denominator are initialized.
  2. Before performing division, the program checks if denominator == 0.
  3. If the condition is true, the goto statement jumps to the error label.
  4. The error message is printed, and the program exits with a non-zero return code indicating an error.

Key Points about goto Keyword

  1. The goto keyword allows unconditional jumps to a labeled statement in the program.
  2. It can simplify breaking out of deeply nested loops or handling errors in certain scenarios.
  3. Using goto can make code harder to understand and debug, so structured alternatives like loops and functions are usually preferred.
  4. Labels must be unique within their scope and followed by a colon (:).
  5. Use goto judiciously and only when other control flow mechanisms are not feasible.