C++ Break statement provides a way to instantly jump out of the loop. After the break statement, the control is immediately shifted to a statement just below the enclosing loop or switch.
It is mostly used within for, while, do-while loops, or switch statements.
Syntax: Break statement
Break;
Flow Chart: Break Statement
Example: Break statement
//break statement in C++
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
cout << i << endl;
}
return 0;
}
Output
Control jumps out of the loop when i=5. Bellow is the output of the above program.
1
2
3
4