Easy Tutorial
❮ Cpp Goto Statement Cpp Examples Sum Natural Number ❯

C++ break Statement

C++ Loops

In C++, the break statement has the following two uses:

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

Syntax

The syntax for the break statement in C++:

break;

Flowchart

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 result:

Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15

C++ Loops

❮ Cpp Goto Statement Cpp Examples Sum Natural Number ❯