C++ Break Statement

 

Break Statement

Break statement is used to break the loop or switch case statements execution and brings the control to the next block of code after that particular loop or switch case it was used in.

 

Break statements are used to bring the program control out of the loop it was encountered in. The break statement is used inside loops or switch statements in C++ language.

 

One such example to demonstrate 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

Here, when i became 6, the break statement got executed and the program came out of the for loop.