goto Statement in C++ (original) (raw)

Last Updated : 16 Jul, 2024

The goto statement in C++ is a control flow statement that provides for an unconditional jump to the same function's predefined label statement. In simple terms, the goto is a jump statement transfers the control flow of a program to a labeled statement within the scope of the same function, breaking the normal flow of a program.

In this article, we will learn about the goto statement and its various use cases in C++.

Syntax of goto in C++

Following is the syntax of the goto statement:

*goto label_name;
_
*._
_
._
_
.**_
// Skipped code
.
.
.
**label_name:
// Execution continues here

The label can be present anywhere in the same scope. For example, in the below code, the label is present before the goto resulting in a loop.

l**abel_name:
// Some code
// ...
.
.
.
**goto label_name; _// Jumps back to the label creating loop

here, **label_name is the unique identifier and goto is the keyword.

Flow Diagram of goto Statement

The following diagram represents the workflow of the goto statement:

goto-Statement-in-Cpp

Examples of goto Statement in C++

Example 1: Simple Program to Demonstrate the Use of goto

C++ `

// C++ Progmra to demonstrate the use of goto #include using namespace std;

int main() { // this statement will be executed cout << "First Statement" << endl;

goto skip_in_between;
cout << "Second Statement" << endl; // will be skipped

skip_in_between:
cout << "Third Statement" << endl;
return 0;

}

`

Output

First Statement Third Statement

**Explanation

Example 2: Creating infinite loop

C++ `

// Example using Syntax 2 #include using namespace std;

int main() { int count = 0;

// Start of the loop

start: cout << "Count: " << count << endl; count++;

// this will create infinite loop
goto start;

cout << "Loop finished" << endl;

return 0;

}

`

**Output

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
_... {till infinite}

**Nore: It is very easy to create the errors such as infinite loops using goto even by mistake so we need to be extra careful while using it. Although in the programming community, the use of goto is highly discouraged.

Example 3: Program to Demonstrate Some Sensible Use goto

C++ `

// C++ program for error Handling using goto statement #include using namespace std;

int main() { FILE* file = fopen("example.txt", "r"); if (!file) { goto error; }

// Perform operations with file
fclose(file);
return 0;

error: cout << "Error opening file" << endl; return 1; }

`

**Output

Error opening file

**Explanation: In the above program goto statement is used for error handling. If the file cannot be opened then the program flow jumps to the **error label and prints an error message for the users before returning 1 to the console.

But this can be easily done with the help of if-else statements also at they provide better risk protection as compared to goto.

Properties of C++ goto Statement

Disadvantages of using goto Statement

Code using goto can be difficult to read, understand and maintaining code with goto statements can be challenging, as it introduces non-linear control flow that may lead to spaghetti code, where the program flow becomes difficult to follow.

For example, try to deduce the control flow of the below program:

C++ `

#include using namespace std;

int main() { int x = 1, y = 2;

start: if (x < 5) { x++; goto check_y; } else { goto end; }

check_y: if (y < 10) { y += 2; goto start; } else { goto process; }

process: cout << "Processing x: " << x << " and y: " << y << endl; if (x + y > 15) { goto cleanup; } else { x++; goto start; }

cleanup: cout << "Cleaning up..." << endl;

end: cout << "End of program." << endl;

return 0;

}

`

Confusing, isn't it? Now imagine a thousand lines of code with hundreds of goto statements.