C++ goto Statement

Introduction

The goto statement in C++ is used to transfer control to a labeled statement within the same function. While it can be useful in certain situations, its use is generally discouraged because it can make code harder to read and maintain. The goto statement can create "spaghetti code," where the flow of the program is difficult to follow. However, understanding how it works can be beneficial for specific use cases.

Syntax

The basic syntax of a goto statement is as follows:

goto label;
// code to be skipped
label:
// code to be executed after the goto statement

Key Points

  • label: A user-defined identifier followed by a colon (:). It marks the location in the code to which control is transferred.
  • goto: The goto statement followed by a label transfers control to the labeled statement.

Example: Basic goto Statement

In this example, we use the goto statement to skip part of the code and jump to a labeled statement.

#include <iostream>
using namespace std;

int main() {
    cout << "Start of program" << endl;

    goto skip; // Transfer control to the label 'skip'

    cout << "This line will be skipped" << endl;

    skip: // Label 'skip'
    cout << "This line will be executed" << endl;

    cout << "End of program" << endl;

    return 0;
}

Output

Start of program
This line will be executed
End of program

Explanation

  • The program starts by printing "Start of program".
  • The goto skip; statement transfers control to the label skip.
  • The line cout << "This line will be skipped" << endl; is not executed.
  • The labeled statement skip: is encountered, and the program continues by printing "This line will be executed" and "End of program".

Example: Using goto for Loop Control

In this example, we use the goto statement to exit a nested loop when a certain condition is met.

#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; // Exit the nested loop when i equals 2 and j equals 2
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    exit_loop: // Label 'exit_loop'
    cout << "Exited the nested loop" << endl;

    return 0;
}

Output

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Exited the nested loop

Explanation

  • The outer loop iterates from i = 1 to i = 3.
  • The inner loop iterates from j = 1 to j = 3.
  • When i equals 2 and j equals 2, the goto exit_loop; statement transfers control to the label exit_loop.
  • The nested loop is exited, and the program continues by printing "Exited the nested loop".

Example: Using goto for Error Handling

In this example, we use the goto statement for simple error handling in a function.

#include <iostream>
using namespace std;

int main() {
    int value = -1;

    if (value < 0) {
        goto error; // Handle error when value is negative
    }

    cout << "Value is positive" << endl;
    return 0;

    error: // Label 'error'
    cout << "Error: Value is negative" << endl;
    return 1;
}

Output

Error: Value is negative

Explanation

  • The program checks if the value is less than 0.
  • If the condition is true, the goto error; statement transfers control to the label error.
  • The program prints "Error: Value is negative" and returns 1, indicating an error.

Conclusion

The goto statement is a control flow tool in C++ that allows you to transfer control to a labeled statement within the same function. While it can be useful for specific use cases like exiting nested loops or simple error handling, it is generally discouraged due to its potential to make code harder to read and maintain. This chapter covered the basic syntax of the goto statement, examples of using it for loop control and error handling, with explanations and outputs. Understanding the goto statement can help you use it judiciously when needed. In the next chapter, we will explore functions in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top