The difference between C and C++ break and continue and how to use them

  • 2020-05-26 09:49:17
  • OfStack

Differences and usage between C/C++ break and continue

break can leave the current switch, for, while, do while block and proceed to the next statement after the block. In switch, it is mainly used to interrupt the comparison of the next case. In for, while, and do while, it is primarily used to break the current loop execution.

The function of continue is similar to that of break, which is mainly used for loop. The difference is that break will terminate the execution of the program block, while continue will only terminate the statement of the program block after it, and jump back to the beginning of the loop block to continue the next loop, instead of leaving the loop.

1.


#include<iostream> 
using namespace std; 
int main() 
{ 
  int i=0; 
  while(i<3) 
  { 
   i++; 
   if(i==1) 
     break; 
   cout<<"i The value is: "<<i<<endl; 
  } 
  return 0;                     
}        

Output result :(empty)

2.


#include<iostream> 
using namespace std; 
int main() 
{ 
  int i=0; 
  while(i<3) 
  { 
    i++; 
    if(i==1) 
     continue; 
    cout<<"i The value is: "<<i<<endl; 
  } 
  return 0;                   
 }

Output result: the value of i is: 2
The value of i is: 3

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: