Ads 468x60px

Saturday, 12 January 2013

break

The break statement is a jump instruction and can be used inside a switch construct, for loop, while loop and do-while loop. The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop. In the loop construct the execution of break statement terminates loop and further execution of the program is reserved with the statement following the body of the loop.

Syntax
break;

Example
#include<iostream.h>
#include<conio.h>

void main()
{
  int i;
  while(1)
  {
    cout<<"Enter 3 to break";
    cin>>i;
    if(i==3)
    {
      cout<<"got 3";
      break;
    }
  }
  getch();
}

Explanation of the program
The control comes inside the loop because in c++ any non zero value is considered as true so the while loop is creating an infinite loop because the expression will always remain true and from the statements within the loop it will always ask to input a value but when we will enter 3 the condition under if will be true and the control will go inside the if block and will print the message got 3 and will break the loop. 
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...