Flow of Control

Break Statement

The break in C++ refers to a loop control statement whose use takes place for the termination of the loop. Furthermore, as soon as the break statement is encountered from within a loop, stopping of the loop iterations takes place there. Moreover, control returns immediately to the first statement after the loop.

Break Statement

Simple Demonstration of Break Statement in C programming

                    

#include 

using namespace std;



int main () {

// Local variable declaration:

int a = 10;



// do loop execution

do {

cout << "value of a: " << a << endl;

a = a + 1;

if( a > 15) {

// terminate the loop

break;

}

} while( a < 20 );



return 0;

}

When the compiling and execution of the above code takes place, the following result is produced:

                    

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

Simple Loops

Consider the situation where one has a need to search an array’s element. In such a situation, one must use a loop to traverse the array beginning from the first index. Furthermore, one must carry out a comparison between the array elements and the given key.
Below is the implementation of this idea:

// CPP program to illustrate

                    

// Linear Search

#include 

using namespace std;



void findElement(int arr[], int size, int key)

{

Furthermore, here, // loop to traverse array and search for key

Moreover, for (int i = 0; i < size; i++) {

if (arr[i] == key) {

cout << "Element found at position: " << (i + 1);

}

}

}



// Driver program to test above function

int main()

{

Furthermore, int arr[] = { 1, 2, 3, 4, 5, 6 };

Moreover, int n = 6; // no of elements

Also, int key = 3; // key to be searched



// Calling function to find the key

findElement(arr, n, key);



return 0;

}

Output:

The running of the above code is fine with no errors. However, the above code is not efficient. Moreover, even after the element is found, the  above code completes all the iterations.

Suppose there is a presence of 1000 elements in the array and the key, whose searching is to take place in present at the first position. In such a case, the execution of 999 iterations will take place by the above approach. Most noteworthy, such iterations are useless and are of no purpose.

To avoid these useless iterations, the use of break statement should take place in the program. Once the break statement is encountered, there would be an immediate return of the control from the loop after the satisfaction of the condition. As such, the use of the break statement would take place with the if condition which facilitates comparison with the key with array elements as demonstrated below:

                    

// CPP program to illustrate

// using break statement

// in Linear Search

#include 

using namespace std;



void findElement(int arr[], int size, int key)

{

Furthermore, here, // loop to traverse array and search for key

Moreover, for (int i = 0; i < size; i++) {

Also, if (arr[i] == key) {

cout << "Element found at position: " << (i + 1);



// using break to terminate loop execution

break;

}

}

}



// Driver program to test above function

int main()

{

int arr[] = { 1, 2, 3, 4, 5, 6 };

int n = 6; // no of elements

int key = 3; // key to be searched



// Calling function to find the key

findElement(arr, n, key);



return 0;

}

 

Output:

                    

Element found at position: 3

Browse more Topics under Flow of Control

Nested Loops

Use of break statement can also take place while working with nested loops. Furthermore, the use of the break statement takes place in the innermost loop. Most importantly, only from the innermost loop will the control come out.

Use of break with nested loops is shown with an example below:

                    

// CPP program to illustrate

Furthermore, // using break statement

// in Nested loops

#include 

using namespace std;



int main()

{



// nested for loops with break statement

// at inner loop

Furthermore, as one can see, for (int i = 0; i < 5; i++) {

Moreover, for (int j = 1; j <= 10; j++) {

if (j > 3)

break;

else

cout << "*";

}

cout << endl;

}



return 0;

}

Output:

 

In the above code, the inner loop is programmed to execute for 10 iterations. However, as soon as the value of j becomes greater than 3, the inner loop ceases to execute. Consequently, the number of iterations of the inner loop gets restricted to 3 only.

An important point to understand is that the iteration of the outer loop remains unaffected. Therefore, the application of break takes place to only the loop within which it is present.

Infinite Loops

The inclusion of break statement can take place in an infinite loop with a condition. Moreover, this condition facilitates the termination of the execution of the infinite loop. One must consider the example of the infinite loop below:

                    

// CPP program to illustrate

// using break statement

// in Infinite loops

#include 

using namespace std;



int main()

{

// loop initialization expression

int i = 0;



// infinite while loop

while (1) {

cout << i << " ";

i++;

}



return 0;

}

FAQs For Break Statement

Question 1: What is the use of break statement in C++?

Answer 1: Break statement in C++ has the following two usages:

  • When the break statement is encountered inside a loop, an immediate termination of the loop takes place. Moreover, the program control resumes, following the loop, at the next statement.
  • Its use can take place for terminating a case in the switch statement.

Question 2: Differentiate between break statement and continue statement in C++?

Answer 2: There is certainly a difference between break and continue in C++ programming language. Furthermore, the use of the break takes place for immediate termination of the loop. In contrast, the use of a continue statement takes place to skip the current iteration of the loop.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.