C++ goto Statement

www.igi‮aeditf‬.com

In C++, the goto statement is used to transfer control to a labeled statement elsewhere in the program. The goto statement can be used to jump from one point in the code to another, skipping over any statements in between.

Here is the basic syntax of the goto statement:

goto label;
...
label: statement;

The goto statement is followed by a label, which is a statement elsewhere in the program that is marked with a unique identifier (the label name). When the goto statement is executed, program control jumps to the labeled statement and continues execution from there.

Here's an example of using the goto statement to jump to a labeled statement:

int count = 0;

start:
if (count < 10) {
    std::cout << count << std::endl;
    count++;
    goto start;
}

In this example, the goto statement is used to jump back to the start label at the beginning of the loop. The loop will execute 10 times, printing the value of count each time.

While the goto statement can be useful in some situations, it can also make code difficult to read and debug. As a general rule, it's best to avoid using goto and instead use structured programming constructs like loops and functions to control program flow.