C++: Explain like I’m five – Part 5: break, continue and goto statement

C++: Explain like I’m five – Part 5: break, continue and goto statement

Hey there, welcome to the 5th part of my series, C++: Explain like I’m five. In the last part, we discussed all about the different types of loops used in C++. Now we will discuss the loop control statements. There are 3 such statements:

  1. break statement
  2. continue statement
  3. goto statement

We will take a look at all of these statements. Let's start with the break statement.

break statement

There are 2 uses of the break statement in C++:

  1. When we use the break statement inside a loop, the loop is immediately terminated. Then the program control resumes at the next statement after the loop.
  2. It is also commonly used to terminate a case in the switch statement.

Note: We already discussed the use of break statement in switch cases in detail in part 3 of this series. Go take a look and refresh your knowledge here.)

Syntax:

break;

Now, let's take a look at how a break statement is used inside a loop!

Using a break statement inside a do-while loop:

#include <iostream>
using namespace std;

int main () {
   // Local variable declaration:
   int b = 5;

   // do loop execution
   do {
      cout << "The value of b is: " << b << endl;
      b = b + 1;
      if( b > 10) {
         // terminating the loop
         break;
      }
   } while( b < 15 );

   return 0;
}

Output:

The value of b is: 5
The value of b is: 6
The value of b is: 7
The value of b is: 8
The value of b is: 9
The value of b is: 10

Using a break statement inside for loop:

#include <iostream>
using namespace std;
int main(){
   int num;
   for (num =200; num >= 10; num --) {
      cout<<"num: "<< num <<endl;
      if (num == 197) {
         break;
      }
   }
   cout<<"Hey, we're out of the loop!";
   return 0;
}

Output:

num: 200
num: 199
num: 198
num: 197
Hey, we're out of the loop!

Using a break statement inside while loop:

#include <iostream>
using namespace std;
int main(){
   int a =10;
   while(a <= 200) {
      cout<<"Value of a is: "<< a <<endl;
      if (a == 12) {
         break;
      }
      a++;
   } 
   cout<<"Hey, we're out of the loop!";
   return 0;
}

Output:

Value of a is: 10
Value of a is: 11
Value of a is: 12
Hey, we're out of the loop!

continue statement

The continue statement's function is quite similar to the break statement. But, instead of forcing termination, it forces the next iteration of the loop to take place. It does so while skipping any code in between.

In for loop, continue causes the conditional test and increment parts of the loop to execute. Whereas, in the while and do-while loops, it results in the program control passing to the conditional tests.

Syntax:

continue;

In order to understand how a continue statement works, let's look at the following examples.

Using a continue statement inside a for loop:

#include <iostream>
using namespace std;
int main(){
   for (int a = 0; a <= 6; a++) {
      if (a == 3) {
        /*This means that when the program flow goes to 3, the continue statement
            would start working and skip it, going to the next part of the iteration, 
            skipping 3. */
          continue;
      }
      cout<< a <<" ";
   }
   return 0;
}

Output:

0 1 2 4 5 6

Using a continue statement inside a do-while loop:

#include <iostream>
using namespace std;

int main () {

   int i = 10;

   do {
      if( i == 15) {
         i = i + 1;
         continue;
      }
      cout << "value of i: " << i << endl;
      i = i + 1;
   } 
   while( i < 20 );

   return 0;
}

Output:

value of i: 10
value of i: 11
value of i: 12
value of i: 13
value of i: 14
value of i: 16
value of i: 17
value of i: 18
value of i: 19

Using a continue statement inside a while loop:

#include <iostream>
using namespace std;
int main(){
   int i = 6;
   while (i >= 0) {
      if (i == 4) {
         i--;
         /* As explained in the above example, here too, when the program flow goes to
             4, the continue statement is triggered, skipping 4 and moving onto
              the next iteration. */ 
         continue;
      }
      cout<<"Value of i: "<< i <<endl;
      i--;
   }
   return 0;
}

Output:

Value of i: 6
Value of i: 5
Value of i: 3
Value of i: 2
Value of i: 1
Value of i: 0

goto statement

This statement transfers the control of a program to a defined label. Usage of the goto statement is highly not recommended as it makes it difficult to trace the program flow control, thus making the program difficult to understand and quite hard to modify. A program that uses a goto statement can easily be modified to remove it.

Syntax:

goto label_name;

Structure of a program using goto statement:

labelA:
...
...
goto labelB;
...
..
labelB:
...

Example:

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

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

Output:

Enter a number: 32
Even Number!

Though a goto statement can be used like how I showed you guys in this example, still, I will repeat again, it is not recommended! But then again, there is one good use of it, that is, to exit from a deeply nested loop.

for(...) {
   for(...) {
      while(...) {
         if(...) goto exit;
         .
         .
         .
      }
   }
}
exit:
cout << "Error in program.\n";

In this program, a break statement would not work as the program will merely exit from the innermost loop. Here, the goto statement works wonders, exiting completely from a deeply nested loop!

Well, I guess that's it for today. Hope you guys are practicing what you are learning. In the next part, we will discuss functions in detail so stay tuned!