goto statement in C/C++

26 Aug, 2019
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 a 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 the 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 the goto statement. Below program explains how to do this:CC++C// C program to check if a number is// even or not using goto statement#include <stdio.h>  // function to check even or notvoid checkEvenOrNot(int num){    if (num % 2 == 0)        // jump to even        goto even;     else        // jump to odd        goto odd;   even:    printf("%d is even", num);    // return if even    return; odd:    printf("%d is odd", num);}  int main() {    int num = 26;    checkEvenOrNot(num);    return 0;}C++// C++ program to check if a number is// even or not using goto statement#include <iostream>using namespace std;  // function to check even or notvoid checkEvenOrNot(int num){    if (num % 2 == 0)    // jump to even        goto even;     else    // jump to odd        goto odd;   even:    cout << num << " is even";    // return if even    return; odd:    cout << num << " is odd";}  // Driver program to test above functionint main(){    int num = 26;    checkEvenOrNot(num);    return 0;}Output:26 is even

C
C++
// C program to check if a number is// even or not using goto statement#include <stdio.h>  // function to check even or notvoid checkEvenOrNot(int num){    if (num % 2 == 0)        // jump to even        goto even;     else        // jump to odd        goto odd;   even:    printf("%d is even", num);    // return if even    return; odd:    printf("%d is odd", num);}  int main() {    int num = 26;    checkEvenOrNot(num);    return 0;}
// C++ program to check if a number is// even or not using goto statement#include <iostream>using namespace std;  // function to check even or notvoid checkEvenOrNot(int num){    if (num % 2 == 0)    // jump to even        goto even;     else    // jump to odd        goto odd;   even:    cout << num << " is even";    // return if even    return; odd:    cout << num << " is odd";}  // Driver program to test above functionint main(){    int num = 26;    checkEvenOrNot(num);    return 0;}
26 is 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.CC++C// C program to print numbers// from 1 to 10 using goto statement#include <stdio.h>  // function to print numbers from 1 to 10void printNumbers(){    int n = 1;label:    printf("%d ",n);    n++;    if (n <= 10)        goto label;}  // Driver program to test above functionint main() {    printNumbers();    return 0;}C++// C++ program to print numbers// from 1 to 10 using goto statement#include <iostream>using namespace std;  // function to print numbers from 1 to 10void printNumbers(){    int n = 1;label:    cout << n << " ";    n++;    if (n <= 10)        goto label;}  // Driver program to test above functionint main(){    printNumbers();    return 0;}Output:1 2 3 4 5 6 7 8 9 10

C
C++
// C program to print numbers// from 1 to 10 using goto statement#include <stdio.h>  // function to print numbers from 1 to 10void printNumbers(){    int n = 1;label:    printf("%d ",n);    n++;    if (n <= 10)        goto label;}  // Driver program to test above functionint main() {    printNumbers();    return 0;}
// C++ program to print numbers// from 1 to 10 using goto statement#include <iostream>using namespace std;  // function to print numbers from 1 to 10void printNumbers(){    int n = 1;label:    cout << n << " ";    n++;    if (n <= 10)        goto label;}  // Driver program to test above functionint main(){    printNumbers();    return 0;}
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.
SonalSrivastava02
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.