C++

Goto statement with example in C++ – Detailed instructions

The goto statement is used to move program control to a certain label. The goto statement’s syntax is as follows:

goto label_name;

Program structure:

label1:
...
...
goto label2;
...
..
label2:
...

There may be any number of goto and label statements in a program. Whenever a goto statement is encountered, the program’s control is transferred to the label specified in the goto statement.

Because they are complicated, make your program far less understandable, and increase the likelihood of errors, goto statements are seldom ever used in programming. You can use the continue and break statements in place of goto.

C++ goto statement illustration

#include <iostream>
using namespace std;
int main(){
   int num; cout<<"Enter a number: "; cin>>num;
   if (num % 2==0){
      goto print;
   }
   else {
      cout<<"Odd Number";
   }

   print:
   cout<<"Even Number";
   return 0;
}


Output:

Enter a number: 42
Even Number

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button