Understanding Break Statement in C++
The break statement is loop control statement that is used for the terminating the loop. These are used in the situations where we do not have the idea about actual number of iterations for the loop.
This article will discuss the Break statement in C++ and how it is used with loops and decision-making statements in C++. We will be covering the following sections:
- Break Statement in C++
- C++ Break Statement with Simple Loops
- C++ Break Statement with Nested Loops
- C++ Break Statement with Infinite Loops
So, without further ado, let us get started!
Break Statement in C++
The break in C++ is a loop control statement used to terminate the loop. As soon as this break statement is encountered within a loop, the loop iterations end abruptly, and the control will return immediately to the next line of code after the loop.
Syntax
The syntax for the break statement is:
break;
Here’s the flowchart of the break statement in C++:
Now, we will understand how the break statement is implemented with –
- Simple loops
- Nested loops
- Infinite loops
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Break Statement with Simple Loops
The break statement in for loop:
for (initialization; condition; update){
//body of the loop
if (condition to break the loop) {break; }}
The break statement in the while loop:
while (condition) {
//body of the loop
if (condition to break the loop) {
break; }}
Break Statement Implementation Examples
Example 1: break with for loop
#include
using namespace std;
int main() { for (int i = 10; i >= 0; i--) { //break condition if (i == 2) { break; } cout << i << " "; }
return 0; }
Output 1:
10 9 8 7 6 5 4 3
In this example, as soon as the break condition i.e., i == 2 is encountered, the for loop terminates.
Hence, the output is only printed from i = 10 and goes on till i = 3.
Explore C++ courses
Example 2: break with while loop
Consider an example of a program that finds the sum of positive integers and breaks the loop as soon as a negative integer is encountered:
#include
using namespace std;
int main() { int num; int sum = 0;
while (true) { //input from the user cout << "Enter a number: "; cin >> num;
//break condition if (num < 0) { break; }
//summation of positive numbers sum += num; }
//display the sum cout << "Sum: " << sum <<" ";
return 0; }
Output 2:
Enter a number: 45Enter a number: 20Enter a number: 15Enter a number: 5Enter a number:-5Sum: 85
In the above-given example, as soon as the break condition i.e., num < 0, is encountered, the while loop terminates.
Hence, the output prints the sum of all positive integers only.
Break Statement with Nested Loops
When the break statement is used in nested loops, it terminates the inner loop and continues to run the outer loop.
Let’s understand with the following example:
#include
using namespace std;
int main() { int num; int sum = 0;
//nested for loops
//outer for loop for (int i = 1; i <= 3; i++) { //inner for loop for (int j = 1; j <= 3; j++) { if (i == 2) { break; } cout << "i: " << i << ", j: " << j << endl; } }
return 0; }
Output:
i: 1, j: 1i: 1, j: 2i: 1, j: 3i: 3, j: 1i: 3, j: 2i: 3, j: 3
In this above-given example, as soon as the break condition i.e., i == 2, is encountered, the inner for loop terminates, and the control flow of the program is handed back to the outer for loop.
Hence, the output prints the values of variables i and j but skips all values where i == 2.
Break Statement with Infinite Loops
The break statement is used in an infinite loop to terminate the execution of the loop if a certain condition returns True.
Let’s understand with the following example:
#include
using namespace std;
int main() { int i = 1; //infinite while loop while (1) { if (i > 10) //break condition break; cout << i << " "; i++; } return 0; }
Output:
1 2 3 4 5 6 7 8 9 10
In the above-given example, as soon as the break condition i.e., i > 10, is encountered, the while loop terminates.
Hence, the output is only printed from i = 1 and goes on till i = 10.
If the break condition was not specified, this would have been an infinite loop executing an infinite number of times until there is no memory left, or you would need to exit the compiler to terminate the program forcefully.
Endnotes
I hope that this article was helpful for you in understanding how the break statement works with loop and decision-making statements in C++. We learned about its implementation with simple for and while loops, nested loops, and infinite loops. If you want to learn more about C++ and solidify your basics, you can explore our page.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio