C++ Break Statement

The C++ break keyword ends the loop structure. It causes a loop to end immediately, instead of waiting for its condition to be false. In case of inner loop, it breaks only inner loop.

Syntax
jump-statement;    
break;

Flowchart:

Cpp Break statement 1
Example

Let's see a simple example of C++ break statement which is used inside the loop.

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

Break Statement with Inner Loop

If you use break statement inside the inner loop it breaks only the inner loop.

Example

Let's see the example code

snippet
#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 1 3 2 1 3 1 3 2 3 3
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +