The C++ goto statement is additionally referred to as jump statement.
Syntax : -
goto label;
............
............
............
label:
statements;
............
............
............
............
............
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.
When goto label; is encountered, the control of program bounces to label: and executes the code beneath it.
![]() |
Goto statement Working |
Example : -
#include
<iostream>
using namespace std;
int main() {
using namespace std;
int main() {
int i = 10;
int j = 20;
if(i > j)
{
else
{
igreater:
}
int j = 20;
if(i > j)
{
goto igreater;
}else
{
goto jgreater;
}igreater:
cout << i <<" is greater";
return;
jgreater:return;
cout << j <<" is greater";
return;
return;
Output:
20 is greater
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 StatementUse 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.
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.
In modern programming, goto statement is taken into account a harmful and a nasty programming practice.
ConversionConversion EmoticonEmoticon