Briefly illustrate the use of break and continue statements in C++

  • 2020-05-05 11:32:34
  • OfStack

In fact, the break statement can also be used in the loop body. The general format of the break statement is:


  break;


Its role is to make the process from the loop body out of the loop body, that is, to prematurely end the loop, and then execute the statements under the loop body. break statements can only be used in looping statements and switch statements, and cannot be used alone or in other statements.

The general format of the continue statement is


  continue;


Its purpose is to end the loop, that is, to skip the following unexecuted statements in the body of the loop, followed by the next decision whether to execute the loop.

The difference between the continue statement and the break statement is that the continue statement only ends the loop, not the entire loop. The break statement, on the other hand, ends the loop and no longer determines whether the conditions for executing the loop are true.

Examples of procedures:

1.


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 :(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: i value: 2
The value of     i is: 3


Related articles: