Latest web development tutorials

C ++ break statement

C ++ loop C ++ loop

C ++ in thebreak statement has the following two ways:

  1. When abreak statement occurs within a loop, the loop will be immediately terminated and the program flow will continue to implement immediately the next statement cycle.
  2. It can be used to terminatethe switch statement in a case.

If you are using a nested loop (ie a loop nested inside another loop), break statement to stop the execution of the innermost loop, and then start the next line of code after the block.

grammar

Break statement in C ++ syntax:

break;

flow chart

C ++ break statement

Examples

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;

   // do 循环执行
   do
   {
       cout << "a 的值:" << a << endl;
       a = a + 1;
       if( a > 15)
       {
          // 终止循环
          break;
       }
   }while( a < 20 );
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15

C ++ loop C ++ loop