Cpp - Using break statement

Introduction

The break statement exits from a switch or loop immediately.

You can use the break keyword to jump to the first statement that follows the switch or loop.

Sample program containing a break statement

Demo

#include <iostream> 
#include <iomanip> 
using namespace std; 
int main() /*  w w w.j  av a2 s.c o m*/
{ 
    int ac = 32;          // To begin with ASCII Code 32 
    while(true) 
    { 
      cout << "\nCharacter   Decimal   Hexadecimal\n\n"; 
      int upper; 

      for( upper =ac + 20; ac < upper && ac < 256; ++ac) 
          cout << "      " << (char)ac     // as character 
                << setw(10) << dec << ac 
                << setw(10) << hex << ac << endl; 
      if( upper >= 256)   break; 
      cout <<"\nGo on -> <return>,Stop -> <q>+<return>"; 
      char answer; 
      cin.get(answer); 
      if( answer == 'q' || answer == 'Q' ) 
        break; 
      cin.sync();            // Clear input buffer 
    } 
    return 0; 
}

Result

Related Topic