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:

goto 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";

   cout<<"Even Number";
   return 0;


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