Tutorials Point


  C++ Basics
  C++ Object Oriented
  C++ Advanced
  C++ Useful References
  C++ Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

C++ break statement


previous next AddThis Social Bookmark Button


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:

break;

Flow Diagram:

C++ break statement

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


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names