C++ GOTO Statement

In C++ , goto statement is used for change the normal sequence of program execution by transferring control to other a part of the program.

The C++ goto statement is additionally referred to as jump statement.

Syntax : -

goto label;
............
............
............
label:
statements;
............
............


In the syntax above,

label is an identifier.

When goto label; is encountered, the control of program bounces to label: and executes the code beneath it.

goto statement
Goto statement Working

Example : -

#include <iostream>
using namespace std;

int main() {
int i = 10;
int j = 20;
if(i > j)
{
goto igreater;
}
else
{
goto jgreater;
}
igreater:
cout << i <<" is greater";
return;
jgreater:
cout << j <<" is greater";
return;
}


Output:
20 is greater
Note:
Use of goto statement is very discouraged because it makes difficult to trace the control flow of a program, making the program hard to know and hard to switch .

Any program that uses a goto are often rewritten in order that it doesn't need the goto.
Reason to Avoid goto Statement

The goto statement gives power to leap to any a part of program but, makes the logic of the program complex.

In modern programming, goto statement is taken into account a harmful and a nasty programming practice.