Use of break statement in C++

- March 13, 2018
C++ provides the break statement to implement middle-exiting control logic. The "break statement" is used to exist from the loop (do, for, while) structure and from the switch structure. It causes immediate exit from the body of loop, in switch structure, it is normally used at the end of statements to terminate the processing of a particular (or each) case. If all "break" statement are omitted from the switch structure, then statements of all remaining cases after the matched case are also executed.

Syntax of break statement

The general syntax of break statement used in C++ do , for, while or switch statements is as follows:
statement(s);

break; 

Example of break statement

The following example of C++ program illustrates the break statement:

#include<iostream.h>

#include<canio.h>

main ()

 {

   char ch;

   clrscr();

   cout<<"Press any key, Q to quit."<<endl;

   cin>>ch;

      if (ch == 'Q')

          break;


   getch();

 }