AppDividend
Latest Code Tutorials

Break Statement in C++ Example | C++ Break Statement Program

0

Break Statement in C++ Example is today’s topic. The break statement is used to terminate the loop. When we use the break statement, the control from the loop iterations stops, and the control returns to the very first statement after the loop. The break; statement terminates a loop (forwhile and do..while loop) and a switch statement immediately when it appears.

Break Statement in C++

We generally use break statements when we are not sure about how much time we want to iterate over a loop.

The break statement is used when we reach a particular condition for which we want to exit from the loop.

For example, I have a variable k whose value is 10. I am running a loop, and I want to return from the loop when the value of k becomes 5.

I have used a decrementing statement also inside the loop i.e k=k-1; Hence, when the when of k will be decremented to 5, I can use if condition there with a break statement inside to immediately exit the loop and the next statement after the loop.

The main application of the break statement is in the switch case.

Break statements are used in three kinds of loops:

Simple loops

In this break, statements are generally used to increase the efficiency of the program as we can take an example of searching an array. 

Nested loops

In case of nested loops if we use break statements only that particular loop will be exited that means if there is a break statement in the inner loop only the control will be exited from the inner loop, not from the outer loop.

Infinite loops

The break can be used in an infinite loop to stop the running of the infinite loop when a particular condition is reached.

Syntax of break statement

break;

In real practice, break statement is almost always used inside the body of the conditional statement (if…else) inside the loop.

See the following example.

for(int i=0;i<=5;i++)
{
	cout<<i<<endl;
			
	if(i==3)
	{
		break;
	}
	i=i+1;	
}

See the output.

0
1
2

Here we can see that run only runs three times as the value of i reaches 3 the break statement is executed, and the loop is terminated at that point.

Flow chart of break statement

 

Break Statement in C++ Example

Example programs on break statement

Q1- Write a program to show the mechanism of a break statement.

#include <iostream>
using namespace std;

int main()
{
  int k = 10;
  cout << "We will break from the loop when the value of variable k will be equal to 5" << endl;
  for (int i = 10; i > 0; i--)
  {
    if (k == 5)
    {
      break;
    }
    cout << "Value of k is: " << k << endl;
    k -= 1;
  }
  cout << "We breaked out from the loop!" << endl;
  return (0);
}

See the output.

 

Example programs on break statement

Q2-Write a program to break from an array search if the number is found.

#include <iostream>
using namespace std;
int main()
{
  int n, f;
  cout << "Enter the elements in array: ";
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
  {
    cout << "Enter element number " << i + 1 << "=";
    cin >> a[i];
  }
  cout << "Enter the number you want to search:";
  cin >> f;
  for (int i = 0; i < n; i++)
  {
    if (a[i] == f)
    {
      cout << "Element found at index:" << i << endl;
      break;
      cout << "We used break so that this line won't run" << endl;
    }
  }
  cout << "Holla !! We breaked out from the loop as soon as we found the element" << endl;
  return (0);
}

See the output.

 

C++ Break Statement Program

Example: break statement in Switch Case

We have already discussed the Switch Case on this blog.

#include <iostream>
using namespace std;

int main()
{
  int num = 11;
  switch (num)
  {
  case 21:
    cout << "Case 1 " << endl;
    break;
  case 11:
    cout << "Case 11 " << endl;
    break;
  case 10:
    cout << "Case 3 " << endl;
    break;
  default:
    cout << "Default " << endl;
  }
  cout << "Hey, I'm out of the switch case";
  return 0;
}

See the output.

Case 11 
Hello, I am from Stranger Things

In this example, we have a break statement after each Case block, and this is because if we don’t have it, then the subsequent case block would also execute.

Finally, the Break Statement in C++ Example | C++ Break Statement Program is over.

Recommended Posts

Constructor Overloading in C++

Function Overloading in C++ Tutorial With Example

C++ Friend Function Example

C++ Destructor Example

C++ String Class Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.