break in C++

The C++ break statement is used to terminate the whole loop if the condition is met. Unlike the continue statement after the condition is met, it breaks the loop and the remaining part of the loop is not executed.

The break statement is used with decision-making statements such as if, if-else, or switch statement which is inside the for loop which can be for loop, while loop, or do-while loop. It forces the loop to stop the execution of the further iteration.

Syntax

break;

Flowchart of break Statement

Example of break Statement

Consider a scenario where the series of a number is to be printed but not after a certain value K. So in this case, the idea is to use the break statement after the value of i is K. Below is the program for the same:

C++




// C++ program to demonstrate the
// break statement
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    for (int i = 1; i < 10; i++) {
 
        // Breaking Condition
        if (i == 5)
            break;
        cout << i << " ";
    }
    return 0;
}


Output

1 2 3 4 

Jump statements in C++

Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continue the loop inside a program or to stop the execution of a function.

Similar Reads

Types of Jump Statements in C++

In C++,  there is four jump statement...

continue in C++

The C++ continue statement is used to execute other parts of the loop while skipping some parts declared inside the condition, rather than terminating the loop, it continues to execute the next iteration of the same loop. It is used with a decision-making statement which must be present inside the loop....

break in C++

...

return in C++

The C++ break statement is used to terminate the whole loop if the condition is met. Unlike the continue statement after the condition is met, it breaks the loop and the remaining part of the loop is not executed....

Goto statement in C++

...