Skip to main content

Break Statement in C++

What is the Break Statement in C++?

The break statement in C++ is a control statement used to exit prematurely from loops (like for, while, or do-while loops) and switch statements. When encountered, the break statement immediately terminates the loop it's contained within, regardless of the loop's normal exit conditions. This allows for an early exit based on certain conditions without executing the remaining code inside the loop.

Constants in CPP

Explain the Syntax of Break Statement in C++.

The syntax of the break statement in C++ is simple and straightforward.It's used without any conditions or expressions. Here's the syntax:

Syntax of break statement:

     break;

When encountered within a loop or a switch statement, the break statement immediately terminates the nearest enclosing loop or switch statement and transfers control to the statement immediately following the terminated loop or switch.

C++ Break Statement Example

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
if (i == 3) {
cout << "Breaking out of the loop when i is 3." << endl;
break;
}
cout << i << " ";
}
return 0;
}

C++ Break Statement in Nested Loop.

The break statement can be especially helpful in C++ when utilising nested loops to manage the execution flow. It lets you break out of nested loops at any number of levels under specific circumstances. The following is an example of how to use break in nested loops:

Example

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 3; ++i) {
cout << "Outer loop iteration: " << i << endl;
for (int j = 1; j <= 3; ++j) {
cout << "Inner loop iteration: " << j << endl;
if (j == 2) {
cout << "Breaking out of the inner loop." << endl;
break; // Exits the inner loop when j equals 2
}
}
}
return 0;
}