Break and Continue in C++

Break and Continue in C++

Break Statement in C++

The break statement in c++ is used to break loop and switch statement. Breaks statement used to breaks the current flow of the program at the given condition. The break statement in C++ terminates the loop when it is meet unexpectedly.

It is also use in switch case control structure after the case blocks

Syntax of break statement 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
break;
break;
break;  

Program of break statement in C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\n";
}
}
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; } cout<<i<<"\n"; } }
#include <iostream>  
using namespace std;  
int main() {  
      for (int i = 1; i <= 10; i++)    
          {    
              if (i == 5)    
              {    
                  break;    
              }    
        cout<<i<<"\n";    
          }    
}  

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
2
3
4
1 2 3 4
1
2
3
4

Continue Statement in C++

The continue statement in C++ is used to continue the loop. Continue  statement is used to continues the current iteration of the program and skips the remaining code at certain condition and control the program goes to next iterarion. It is used to move to the next iteration.

Syntax of continue statement 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
continue;
continue;
continue;

Program of continue statement in C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
cout<<i<<"\n";
}
}
#include <iostream> using namespace std; int main() { for(int i=1;i<=10;i++){ if(i==5){ continue; } cout<<i<<"\n"; } }
#include <iostream>  
using namespace std;  
int main()  
{  
     for(int i=1;i<=10;i++){      
            if(i==5){      
                continue;      
            }      
            cout<<i<<"\n";      
        }        
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
2
3
4
6
7
8
9
10
1 2 3 4 6 7 8 9 10
1
2
3
4
6
7
8
9
10

 

Read more, Control Statements in C++

Share this post

Leave a Reply

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