C++ goto statements



A goto statement used as unconditional jump from the goto to a labeled statement in the same function.

C++ goto statement :

  1. goto statement is used for unconditional jump.
  2. goto statement makes difficult to trace the control flow of program
  3. goto statement should be avoided in order to make smoother program.

Syntax :

goto label;
..
.
label: statement;

Example #1 : Goto Statement :

#include <iostream>
using namespace std;
 
int main ()
{
   int num = 1;
   STEP:do
   {
     if( num == 5)
     {
        num = num + 1;
        goto STEP;
     }

     cout << "value of num : " << num << endl;
     num = num + 1;

   }while( num < 10 );
   return 0;
}

Output :

value of num : 1
value of num : 2
value of num : 3
value of num : 4
value of num : 6
value of num : 7
value of num : 8
value of num : 9

Different types of goto :

cpp goto statement