goto statement in C++


Reading time: 15 minutes | Coding time: 5 minutes

In C++, the goto statement is a jump statement used to transfer unconditional control to a specified label. In other words, it allows you to jump from any starting point to any ending point in a program, altering the program's flow.

Structure/Syntax

The goto statement in C++ can be implemented in the following two ways:

  1. Corresponding label is after the goto statement
  goto label1; // goto statement 
  ...
  ...
  label1:  // label 

When the goto statement is executed, the program will jump to the corresponding label below, skipping any lines in between.

  1. Corresponding label is before the goto statement
  label1:  // label 
  ...
  ...
  goto label1; // goto statement 

When the goto statement is executed, the program will jump to the corresponding label above.

Flowchart

Screen-Shot-2020-03-20-at-3.18.12-PM

Above is a flowchat for the goto statement to help you visualize how it works. After the first goto statement is executed, the next statements are skipped. The program resumes executing statements at the corresponding label (in this case, Label 3).

Example 1

#include <iostream>;
using namespace std;

int main(){
    // declares variable n
    int n = 4;
    
    if ( n%2 == 0 ){
        // goto statement 
        goto even; 
    }
    
    cout << "Hello" << endl;
    
    // label
    even: 
    cout << "The number is even" << endl;
}

Output:

This number is even

When n is 4, n % 2 = 0 so the body of the if statement is executed. The goto statement alters the sequence of the program so that everything after the corresponding label of "even:" is executed. In other words, the program jumps to the "even:" label statement. The code in between the goto statement and its label is skipped so "Hello" is not printed.

Example 2

#include <iostream>
using namespace std;

int main(){
    
    // declares variable i
    int i = 0;
    
    // label
    loop: 
    i++;
    
    if ( i < 10 ){
        // prints out i
        cout << i << " ";
        
        // goto statement 
        goto loop; 
    }
   
}

Output:

1 2 3 4 5 6 7 8 9

Here, the goto statement helps form a loop. When i < 10, i is printed out and the goto statement is executed. This causes the flow of the control to transfer to the label "loop:" above. Then, i is incremented and the body of the if-statement is executed again. This process repeats until i >= 10, which happens after the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 have been printed out.

To summarize, the following steps are repeated until i >= 10

  1. The variable i is incremented by 1
  2. The variable i is printed out
  3. The program flow shifts to the corresponding label of the goto statement

Example 3

In the following example, goto statements are used in a function to determine whether a given number is a multiple of 5:

#include <iostream>
using namespace std;

// function to check if a multiple of 5 or not 
void multipleOf5(int n) 
{ 
  if (n % 5 == 0) {
    // goto statement
    goto isMultiple;
  }
  else {
    // goto statement
    goto notMultiple;
  }
  
  // "isMultiple" label
  isMultiple: 
    cout << num << " is a multiple of 5." << endl; 
    return; // return if number is a multiple of 5

  // "notMultiple" label
  notMultiple: 
    cout << num << " is not a multiple of 5." << endl; 
} 
  
// Driver code
int main() { 
  // test function for values of 25 and 103
  int n1 = 25; 
  int n2 = 103;
  multipleOf5( n1 ); 
  multipleOf5( n2 ); 

} 

Output:

25 is a multiple of 5.
103 is not a multiple of 5. 

Disadvantages of the goto Statement \

Although the goto statement may seem useful and practical, the use of the goto statement is discouraged for the following reasons:

  1. It can make the program complex and hard to follow.
  2. It makes it difficult to modify and alter the program.
  3. It is not necessary to use them (any program can be rewritten without goto statements).

Breaking out of nested loops

The goto statement can be used to break out of nested loops.

Here is an example that shows how the goto statement can be used to break out of a nested loop:

#include <iostream>
using namespace std;

int main(){

    int i = 0, j = 0, k = 0;
    int ar1 [5] = { 1, 2, 3, 4, 5 };
    int ar2 [5] = { 1, 2, 3, 4, 5 };
    int ar3 [5] = { 2, 3, 4, 5, 6 };
    
    for ( i = 0; i < 5; i++) {
        for ( j = 0; j < 5; j++) {
            for ( k = 0; k < 5; k++) {
                if ( ar1[i] == ar2[j] and ar2[j] == 1 + ar3[k]) {
                    // goto statement
                    goto end;
                }
            }
        }
    }
    
    end: // corresponding label
    cout << i << " " << j << " " << k << endl;
}

Output:

2 2 0

Other potential and more common applications of goto statements include using them to:

  • cleanly exit a function
  • manage errors

However, like mentioned before, it is generally considered better to avoid using goto statements in your code.

Question

Which of the following is not true about the goto statement?

It can alter the program's sequence
It can be used to create loops
You should avoid using it
The label is always after the goto statement
The corresponding label does not need to be after the goto statement. Even if it is before the goto statement, the control of the program will still transfer to the label. This idea can be used to create loops, which is shown in Example #2 above.

With this article at OpenGenus, you will have a complete idea of using goto statements in C++. Enjoy.