C++ break statement

« Previous Chapter Next Chapter »

The break statement has following two usage in C++:

  1. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.

  2. It can be used to terminate a case in the switch statement (covered in the next chapter).

If you are using nested loops ( ie. 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 of a break statement in C++ is:

Example

break;
                                        

Flow Diagram:

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, Output Will Be :

Example

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


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!