Understanding Break Statement in C++

Understanding Break Statement in C++

3 mins read1.8K Views Comment
Updated on Aug 2, 2024 13:58 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.

about break statement in C++

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
Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
2 hours
Free
9 hours
Free
3 hours
Free
4 hours

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
using namespace std;
int main() {
for (int i = 10; i >= 0; i--) {
//break condition
if (i == 2) {
break;
}
cout << i << " ";
}
return 0;
}
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
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;
}
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++
The Quicksort algorithm in C++ is an efficient sorting technique that uses a divide-and-conquer approach. It works by selecting a 'pivot' element, partitioning the array into two sub-arrays around the...read more
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
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;
}
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
using namespace std;
int main() {
int i = 1;
//infinite while loop
while (1) {
if (i > 10) //break condition
break;
cout << i << " ";
i++;
}
return 0;
}
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.

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