The break statement is used to terminate the execution of a loop or switch case statement and move control to the next block of code following the loop or switch case in which it was used.
Break statements are used to exit the loop in which the programme control was encountered. In C++, the break statement is used inside loops or switch statements. One such illustration of how a break statement works is
#include <iostream>
using namespace std;
int main()
{
int num = 10;
int i;
for (i = 0; i < num; i++)
{
if (i == 6)
{
break;
}
cout << i << " ";
}
return 0;
}
Output:
0 1 2 3 4 5
When I reached 6, the break command was performed, and the programme exited the for loop.