Examples of goto statement in c++ programing

0

the goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Example :

goto label ;

... ..   ...

... ..  ...

... ..  ...

 label ;

statement ;

All Contributions

Write a program with goto statement and continue :

#include<iostream.h>
using namespce std;
 int main()
   {
     for(int i=0; i<=20; i++)
      {
         if(i%2==0)
             goto even; // even number
         else
             continue; // odd number skip
        even:
          cout<<i<<"\t";
      }
return 0;
  }

Write a program with goto statement and break statement:

#include<iostream.h>
using namespace std;

 int main()
  {
   int num1,num2,sum;
    
    start:
      cout<<"\nEnter two number: ";
      cin>>num1>>num2;

       if(num1== 0 || num2==0) 
           break;

      sum = num1+num2;
      cout<<"Total: "<<sum;
  
    goto start;

  cout<<"this is next statement";
    return 0 ;
 }

Write a Program that find out whether a given number is equal or not:

#include<iostream>
using namespace std;
 int main()
  {
     int num1,num2;
     cout<<"Enter Two number: ";
     cin>>num1>>num2;
       if(num1==num2) 
            goto transfer;  
       else
            cout<<"Given number are not Equal";            
    transfer: {
        cout<<"\nGiven number are Equal "<<x;
      }
 return 0;
 }

Write a program using goto where we will print numbers from 1 to 10 and the place of 10 numerically we will print it alphabetically :

#include <iostream>
using namespace std;
int main()
{
  for (int i = 1; i <= 10; i++)
  {
    if (i == 10)
    {
      goto q;
    }
    cout << i << " "
         << "Element=" << i << endl;
  }
q:
  cout << "I am the 10th element = ten";

         return 0 ;
}

Write a program using goto where we will print numbers from 1 to 10 and the place of 10 numerically we will print it alphabetically :

#include <iostream>
using namespace std;
int main()
{
  for (int i = 1; i <= 10; i++)
  {
    if (i == 10)
    {
      goto q;
    }
    cout << i << " "
         << "Element=" << i << endl;
  }
q:
  cout << "I am the 10th element = ten";

         return 0 ;
}

total contributions (6)

New to examplegeek.com?

Join us