Statement of Break

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.

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in