Updated March 17, 2023
Introduction to Break Statement in C++
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop. In this article, we are going to see the concept of a break.
The break statement is used in the following scenario:
- When a user is not sure about the number of iterations in the program
- When a user wants to stop the program based on some condition.
Syntax:
break Keyword is used to represent the break statement.
break;
Flowchart:
Flowchart for break statement in C++ language is as follows:
How does Break Statement work in C++ Language?
The break statement terminates the loop where it is defined and execute the other. If the condition is mentioned in the program, based on the condition, it executes the loop. In the Flowchart diagram, you can see that the first checks the condition. If the condition is true, it executes the conditional statement, and if the break statement is mentioned, it will immediately break the program. otherwise, the loop will iterate until the given condition fails. if the condition is false, it stops the program.
Examples of Break Statement in C++
To understand the concept of break statement in c++, we will see some examples.
Example #1 – Use of Break Statement in for loop
The program for using Break statement in for loop is given as follows:
Code:
#include<iostream>
using namespace std;
int main()
{
int i;
for(i = 0; i <= 10; i ++)
{
cout << i;
if(i ==8)
{
break;
}
}
return 0;
}
Explanation of the above code
Here we have used a break statement in for loop. We have written a program to print numbers from 0 to 10. First, we initialize the i variable in for loop; we have to initialize the i variable to 0 to start the loop at 0 to print until 10; we have mentioned the condition less than equal to 10. And to increment the number, we have used increment. Inside for loop, the cout is used to print the value of i. To break the loop at 8, we have used the if statement to check if the number is equal to 8 breaks the loop and transfer the control outside the loop.
Output :
Example #2 – Use of Break Statement in while loop
Program for using Break statement in while loop is given as follows.
Code:
#include<iostream>
using namespace std;
int main()
{
int i = 10;
while(i <= 100)
{
i = i * 2;
cout << i <<endl;
if(i == 80)
{
break;
}
}
return 0;
}
Explanation of the code
Here we have used break statements in a while loop. Here we have written a program to print the value of 1 by multiplying it by 2. First, we have initialized 1 to 10 and mentioned a condition using a while loop to check if the number is less than or equal to 100. If the condition is true, it will print the value of i. The break statement is mentioned to break a value at 80. Up to 80, the loop will iterate by executing the statement. When the break statement condition match, the program will break.
Output:
Example #3 – Use of Break Statement in switch Case
The program for using Break statement in Switch Case is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a value between 1 to 5";
cin >> n;
switch(n)
{
case 1: cout <<"You have entered 1" << endl;
break;
case 2: cout <<"You have entered 2" << endl;
break;
case 3: cout <<"You have entered 3" << endl;
break;
case 4: cout <<"You have entered 4" << endl;
break;
case 5: cout <<"You have entered 5" << endl;
break;
default: cout << "Please enter valid value";
break;
return 0;
}
}
Explanation of the code
Here we have used a break statement for the switch statement. A switch statement is used to check the case and print the value. Here we have written a program to allow a user to enter a value between 1 to 5. A switch statement is used to check the condition. If the user enters a number from 1 to 5, it will execute the corresponding case. If the user enters value except 1 to 5, it will execute the default condition. The break statement is used in Every case statement to break the statement.
Output:
Conclusion
In this article, we have seen how to use Break Statement in C++ using For loop, While loop, Switch Case, and their programs and explanation. I hope you’ll find this article helpful to understand the use of the Break Statement.
Recommended Articles
This is a guide to Break Statement in C++. Here we discuss syntax, flowchart, use break statement in C++ using For loop, While loop and Switch case, etc. You may also look at the following articles to learn more-