C++ break Statement
In C++, the break statement has the following two uses:
When the break statement appears inside a loop, the loop is immediately terminated, and the program flow continues with the next statement following the loop.
It can be used to terminate a case in a switch statement.
If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Syntax
The syntax for the break statement in C++:
break;
Flowchart
Example
#include <iostream>
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 above code is compiled and executed, it produces the following result:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15