C++ break Statement

A featured image depicting C++ break statement

Purpose of break statement

The break statement is used to exit the for, while, do-while loops, and switch statements.

  • As we iterate through a C++ while or for loop and the break statement is executed, the control moves outside of the loop to the next statement (if written).
  • In case of break statement executes inside the inner loop, it exits only the inner loop.

Syntax of using break statement

The break statement is used as follows:

break;

An example of a break statement in for loop

In the following example,

  • we initialized a variable x with the value of 1.
  • The condition is set to keep on iterating the for loop till the value of variable x reaches 10.
  • In each iteration, the value of x is incremented by 1.
  • However, inside the for loop (inside curly braces), we used a break within the if statement.
  • The if statement checks the value of x and as it reaches 5, the condition evaluates as true, and the break statement executes.

See the code and output:

#include <iostream>

using namespace std;

int main() {

  for (int x = 1; x <= 10; ++x) {

  if (x == 5) {

    break;

  }

     cout << "Current value of x= " << x << endl;

  }

  return 0;

}

CPP-break-for-loop

For clarification, this would be output if we had not used the break statement:

CPP-break-for-loop-2

Using break in the while loop example

As such, a while loop is used to execute a block of code until the given condition is true. However, you may exit the while loop in between by using the break statement.

In the following example,

  • The while loop is supposed to execute until the value of variable x is less than or equal to 10.
  • However, we used the break statement to exit the loop as x value reached 5
#include <iostream>
using namespace std;

int main() {
  int x = 1;

  while (x <= 10) {

  if (x == 5) {
    break;
  }

    cout << "Value of x = " << x << "\n";
    x++;
  }

  return 0;

}

CPP-break-while-loop

If we did not use the break statement, the following would have been output:

CPP-break-while-loop-2

An example of the break with do while loop

Just like a while loop, you may break a do while loop by using the break statement. Have a look at an example below:

#include <iostream>

using namespace std;

int main() {

   int x=0;
   do {
      if (x==5) {
         break;
      }

      cout << "Value of x = "<< x << "\n";
      x++;
   } while (x <= 10);
}

Output:

Value of x = 0
Value of x = 1
Value of x = 2
Value of x = 3
Value of x = 4

In the example,

  • We initialized the variable x=0
  • In the do while loop, we wrote a condition to check if value of the variable x = 5
  • As the condition became true, the break statement executed and it exited the loop

Using the break with the switch statement

Although, the break statement is optional in the C++ switch/case statement. It is normally used with each case.

After the switch expression is checked once, its value is compared with the case.

As soon as a case is matched, the code inside it executes and then we exit the switch statement by using the break statement.

The following example shows the usage of the break in the switch case statement:

#include <iostream>

using namespace std;

int main() {

  int color = 4;

  switch (color) {

  case 1:

    cout << "The color is Red";

    break;

  case 2:

    cout << "The color is Yellow";

    break;

case 3:

    cout << "The color is Blue";

    break;

case 4:

    cout << "The color is Green";

    break;

default:

    cout << "Some other color";

  }

  return 0;

}

Output:

The color is Green

The example of C++ break with nested for loop

As mentioned in the introductory section, if you use the break statement inside the inner for loop, it will exit only the inner for loop while the outer loop keeps on executing.

See its usage in the example below:

#include <iostream>

using namespace std;

int main()

{

//Output loop

for(int x=1;x<=5;x=x+2){

  //Inner loop

  for(int y=1;y<=6;y=y+2){




  //break the loop as value of x and y = 4

  if(x==4&&y==4){

    break;

 }

   cout<<x<<" "<<y<<"\n";

 }

}

}

Output:

1 1
1 3
1 5
3 1
3 3
3 5
5 1
5 3
5 5

In the above code:

  • An outer for loop is initiated with the value of variable x=1
  • There we incremented the value by x by 2 in each iteration.
  • Similarly, in the inner for loop, we initiated the variable y=1
  • and increment the value of y by 2 in each iteration.
  • Inside the inner for loop, we placed and condition if the value of variables x and y are 3, the break statement will execute.
  • You can see in the output, the 3 3 is not displayed.
  • However, the outer loop kept on executing.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!