Understanding Break Statement in C++

Understanding Break Statement in C++

3 mins read1.8K Views Comment
Updated on Jan 16, 2023 17:32 IST

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.

2022_07_Understanding-Break-Statement-in-C.jpg

Loops are used in a programming language to execute a specific block of code repeatedly. In C++, there are three types of Loops:

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:

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;
Copy code

Here’s the flowchart of the break statement in C++:

2022_07_flow-chart-of-break-statement-in-C.jpg

Now, we will understand how the break statement is implemented with –

  • Simple loops
  • Nested loops
  • Infinite loops

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;
}
}
Copy code

The break statement in the while loop:

 
while (condition) {
//body of the loop
if (condition to break the loop) {
break;
}
}
Copy code

Break Statement Implementation Examples

Example 1: break with for loop

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
for (int i = 10; i >= 0; i--) {\n \n
//break condition \n \n
if (i == 2) {\n \n
break;\n \n
}\n \n
cout << i << " ";\n \n
}\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output 1:

 
10 9 8 7 6 5 4 3
Copy code

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
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int num;\n \n
int sum = 0;\n \n
\n \n
while (true) {\n \n
//input from the user\n \n
cout << "Enter a number: ";\n \n
cin >> num;\n \n
\n \n
//break condition\n \n
if (num < 0) {\n \n
break;\n \n
}\n \n
\n \n
//summation of positive numbers\n \n
sum += num;\n \n
}\n \n
\n \n
//display the sum\n \n
cout << "Sum: " << sum <<" ";\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output 2:

 
Enter a number: 45
Enter a number: 20
Enter a number: 15
Enter a number: 5
Enter a number:-5
Sum: 85
Copy code

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.

C++ if else Statement
C++ if else Statement
In this article, we will learn how decision-making is done in C++ by using conditional statements. These statements work on the logic that a block of code will be executed...read more
Introduction to QuickSort Algorithm in C++
Introduction to QuickSort Algorithm in C++
In this tutorial, we are going to learn about a common sorting technique – QuickSort, and we are going to see how this technique is implemented in C++.
Tokens in C Programming
Tokens in C Programming
Tokens are the smallest unit in the C program. Every keyword or character or sequence of characters that you come across in C is a token. It is the smallest...read more

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
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int num;\n \n
int sum = 0;\n \n
\n \n
//nested for loops\n \n
\n \n
//outer for loop\n \n
for (int i = 1; i <= 3; i++) {\n \n
//inner for loop\n \n
for (int j = 1; j <= 3; j++) {\n \n
if (i == 2) {\n \n
break;\n \n
}\n \n
cout << "i: " << i << ", j: " << j << endl;\n \n
}\n \n
}\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 3, j: 1
i: 3, j: 2
i: 3, j: 3
Copy code

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
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int i = 1;\n \n
\n \n
//infinite while loop\n \n
while (1) {\n \n
if (i > 10) //break condition\n \n
break;\n \n
\n \n
cout << i << " ";\n \n
i++;\n \n
}\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
1 2 3 4 5 6 7 8 9 10
Copy code

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.

_______________

Recently completed any professional course/certifications from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online.

About the Author

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