C++ Break Statement

Break statement ends the execution of surrounding loop. In other words it breaks the loop even before the loop condition becomes false.

You can apply break statement to while loop, do-while loop, for loop and a switch statement.

In this tutorial, we shall go through examples illustrating C++ looping statements with break statements in them.

Syntax of Break

Following is the syntax of break statement.

break;

Please note that you can use break statement only within a loop or switch statement.

C++ Break in While Loop

Break statement can be used to break a C++ While Loop abruptly.

In the following example, while loop tries to print numbers from 0 to 9. But during fourth iteration when i becomes 4, break statement ends the execution of this while loop.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i=0;
   while (i < 10) {
      if (i==4) {
         break;
      }
      cout << i << "  ";
      i++;
   }
}

Output

0  1  2  3

C++ Break in Do-while Loop

Break statement can be used to break a C++ Do-While Loop abruptly.

In the following example, do-while loop tries to print numbers from 0 to 9. But during fifth iteration when i becomes 5, break statement ends the execution of this while loop.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i=0;
   do {
      if (i==5) {
         break;
      }
      cout << i << "  ";
      i++;
   } while (i < 10);
}

Output

0  1  2  3  4

C++ Break in For Loop

You can break a C++ For Loop abruptly using a break statement.

In the following example, we have written a for loop to print numbers from 0 to 9. Also, we have conditionally employed a break statement to execute when the number reaches 7. When break statement executes, the program control comes out of the surrounding for loop.

C++ Program

#include <iostream>
using namespace std;

int main() {
   for (int i=0; i < 10; i++) {
      if (i==7) {
         break;
      }
      cout << i << "  ";
   }
}

Output

0  1  2  3  4  5  6

C++ Break in Switch Statement

In C++ Switch Statement, break statement is usually written at the end of each case block.

Following is an example C++ program with Switch statement containing break statements.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int day = 25;
   switch (day%7) {
      case 1: {
         cout << "Monday" << endl;
         break;
      }
      case 2: {
         cout << "Tuesday" << endl;
         break;
      }
      case 3: {
         cout << "Wednesday" << endl;
         break;
      }
      case 4: {
         cout << "Thursday" << endl;
         break;
      }
      case 5: {
         cout << "Friday" << endl;
         break;
      }
      default: {
         cout << "Off day" << endl;
      }
   }
}

Output

Thursday

Break Statement in Nested Loop

Break statement affects only its surrounding loop. So, if you are using a break statement in a nested loop, please note that it breaks only its surrounding loop.

In the following example, we shall write a break statement inside nested while loop. The program prints a pattern.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i = 1;
   while (i <= 5) {
      int j = 1;
      while (j <= 5) {
         if(j>i) {
            break;
         }
         cout << " *";
         j++;
      }
      cout << "\n";
      i++;
   }
}

Output

*
 * *
 * * *
 * * * *
 * * * * *

Break Statement in If Else Block

We know that break statement could be written only inside a looping statement or switch-case statement.

In the following example, we shall write a break statement inside C++ If Else statement, with no loop or switch wrapping this if-else statement.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int n = 20;
   if (n%2 == 0) {
      if(n%10 == 0) {
         break;
      }
      cout << "something even" << endl;
   } else {
      cout << "odd" << endl;
   }
}

Output

d:\workspace\cpp\main.cpp:8:10: error: break statement not within loop or switch
    8 |          break;
      |          ^~~~~
The terminal process terminated with exit code: 1

We got a compilation error stating that break statement is not within loop or switch.

Conclusion

In this C++ Tutorial, we learned about break statement, what it does to surrounding loop statements, with example C++ programs.