MASIGNASUKAv102
6510051498749449419

C++ Break Statement - Learn With AVRK

Add Comments
Monday, August 30, 2021

Break Statement in C++ Language 

Break statement is used to break a loop or switch statement. It breaks the current flow of the program in the given condition. In the case of inner loop, it only breaks the inner loop.

Syntax -

jump-statement;
break;


Flowchart -

Loading...

Break Statement Example -

Let's look at a simple example of a break statement that is used inside a loop.

#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 10; i++)
    {
        if (i == 5)
        {
            break;
        }
        cout<<i<<"\n";
    }
}

Output -

1
2
3
4

Break Statement with Inner Loop 

The break statement breaks the inner loop only when you use the break statement inside the inner loop.

Let's look at example code -
#include<iostream>
using namespace std;
int main()
{
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++){
            if(i==2&&j==2){
                break;
             }
            cout<<i<<" "<<j<<"\n";
        }
    }
}

Output -

1 1
1 2
2 1
1 3 3 1
3 3
3 2

Also check out this : C++ Do-While Loop - Learn With AVRK
Learn With AVRK

Learn With AVRK