goto statement in C/C++
The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.
Syntax:
Syntax1 | Syntax2 ---------------------------- goto label; | label: . | . . | . . | . label: | goto label;
In the above syntax the first line tells the compiler to go to or jump to the statement marked as label. Here label is a user defined identifier which indicates the target statement. The statement immediately followed after ‘label:’ is the destination statement. The ‘label:’ can also appear before the ‘goto label;’ statement in above syntax.

Below are some examples on how to use goto statement:
Examples:
-
Type 1: In this case we will see a situation similar to as shown in Syntax1 above. Suppose we need to write a program where we need to check if a number is even or not and print accordingly using goto statement. Below program explains how to do this:
// C/C++ program to check if a number is// even or not using goto statement#include <iostream>usingnamespacestd;// function to check even or notvoidcheckEvenOrNot(intnum){if(num % 2 == 0)gotoeven;// jump to evenelsegotoodd;// jump to oddeven:cout << num <<" is evenn";return;// return if evenodd:cout << num <<" is oddn";}// Driver program to test above functionintmain(){intnum = 26;checkEvenOrNot(num);return0;}chevron_rightfilter_noneOutput:
26 is even
Note the return statement after ‘even:’ in checkEvenOrNot function. Once the control jumps to the label with even:, the program will execute every piece of code after it. So it is important to return if the number is even. Otherwise, the code after label odd will also get executed as it comes after even.
-
Type 2:: In this case, we will see a situation similar to as shown in Syntax1 above. Suppose we need to write a program which prints numbers from 1 to 10 using the goto statement. Below program explains how to do this.
// C/C++ program to print numbers// from 1 to 10 using goto statement#include <iostream>usingnamespacestd;// function to print numbers from 1 to 10voidprintNumbers(){intn = 1;label:cout << n <<" ";n++;if(n <= 10)gotolabel;}// Driver program to test above functionintmain(){printNumbers();return0;}chevron_rightfilter_noneOutput:
1 2 3 4 5 6 7 8 9 10
Disadvantages of using goto statement:
- The use of goto statement is highly discouraged as it makes the program logic very complex.
- use of goto makes the task of analyzing and verifying the correctness of programs (particularly those involving loops) very difficult.
- Use of goto can be simply avoided using break and continue statements.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Print a character n times without using loop, recursion or goto in C++
- Switch Statement in C/C++
- Break Statement in C/C++
- Continue Statement in C/C++
- Nested switch statement in C++
- Print "Even" or "Odd" without using conditional statement
- Interesting facts about switch statement in C
- return statement vs exit() in main()
- Programming puzzle (Assign value without any control statement)
- Implementing ternary operator without any conditional statement
- Data type of case labels of switch statement in C++?
- asctime() and asctime_s() functions in C with Examples
- time.h header file in C with Examples
- C++ program to compare two Strings using Operator Overloading
Improved By : SonalSrivastava02



