C++ break Statement

C++ Loops

In C++breakThe statement has the following two usages:

  1. WhenbreakWhen the statement appears within a loop, the loop terminates immediately, and the program flow will continue with the next statement following the loop.
  2. It can be used to terminateswitchA case within the statement.

If you are using nested loops (i.e., one loop nested inside another), the break statement will stop the execution of the innermost loop, and then begin executing the next line of code after that block.

Syntax

In C++breakthe syntax of the statement:

break;

Flowchart

C++ break 语句

Example

Example

#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;
}

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++ Loops

other extensions