goto statement in C and C++
goto statement in C and C++
The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used to jump from anywhere in general conditions.
NOTE ? the use of goto statements in any programming language is strongly discouraged because it makes it difficult to track a program's control flow, making it very difficult to understand the program and difficult to modify.
Syntax:
Syntax1 | Syntax2 ---------------------------- goto label_name; .. .. .. label_name: C-statements
The first line in the syntax above tells the compiler to go, or jump to, the statement marked as a label. The label here is a user-defined identifier indicating the target declaration. The statement followed immediately after 'label:' is the state of destination. In the above syntax, the 'label:' may also appear before the 'goto label;' statement.

Example:
1. We shall see a similar situation in this case to the one shown above in Syntax1. Assume we have to write a program where we have to check whether there is a number even or not and use the goto statement to print consequently.The program below explains how:
// C++ program to check if a number is // even or not using goto statement #include <iostream> using namespace std; // function to check even or not void 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 function int main() { int num = 35; checkEvenOrNot(num); return 0; }
Output:

Example in C:-
// C program to check if a number is // even or not using goto statement #include <stdio.h> // function to check even or not void 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 = 35; checkEvenOrNot(num); return 0; }
Output:

2. In this case, we can see a similar situation to the one seen in Syntax1 above. Suppose we have to write a program using the goto statement, which prints numbers from 1 to 10. The program below explains how to do so.
// C++ program to print numbers // from 1 to 10 using goto statement #include <iostream> using namespace std; // function to print numbers from 1 to 10 void printNumbers() { int s = 5; label: cout << s << " "; s++; if (s <= 20) goto label; } // Driver program to test above function int main() { printNumbers(); return 0; }
Output:

Example in C:
// C program to print numbers // from 5 to 20 using goto statement #include <stdio.h> // function to print numbers from 5 to 20 void printNumbers() { int s = 5; label: printf("%d ",s); s++; if (s <= 20) goto label; } // Driver program to test above function int main() { printNumbers(); return 0; }
Output:

Disadvantages of using goto Statement:
The use of the goto statement is highly disincentive as it makes the logic of the program very complex.
The use of goto makes it very difficult to evaluate and verify the correctness of the programs (especially those that require loops).
Using break and continue statements can simply avoid the use of goto.