C++ break Statement

In C++, the break statement is used to immediately exit a loop. When the break statement is encountered inside a loop, the loop is terminated and program execution resumes at the next statement after the loop.

The break statement is commonly used in loops to exit early when a certain condition is met. Here's an example of using the break statement in a while loop:

int count = 0;

while (count < 10) {
    if (count == 5) {
        break;
    }
    std::cout << count << std::endl;
    count++;
}
Source‮gi.www:‬iftidea.com

In this example, the while loop will execute until the value of count reaches 5. When count is equal to 5, the break statement is executed, which immediately exits the loop. The code after the loop (count++) is not executed, because program execution resumes at the next statement after the loop.

The output of this code will be:

0
1
2
3
4

Note that the break statement can only be used inside a loop or a switch statement. If you try to use break outside of these contexts, the compiler will generate an error.