C++Tutorials

C++ break control statement

In this article, we will learn about c++ break control statement, want’s to know about the control statement click here.

Before moving I want to recap that control stament has two types-:

  1. Conditional Statement
  2. Unconditional Statement

If you want’s to know what is conditional statement, click here .

C++ break statement comes under unconditional control statement.

The C++ break is used to break loop or switch statement. It breaks the current flow of the program at the given condition. In case of inner loop, it breaks only inner loop.

Syntax -:

jump-statement;      
break;  

Example 1 break control statement

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int a = 10;

   // do loop execution
   do {
      cout << "value of a: " << a << endl;
      a = a + 1;
      if( a > 15) {
         // terminate the loop
         break;
      }
   } while( a < 20 );
 
   return 0;
}

Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

Example 2 break control statement

#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

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button