The "Break" statement

The "Break" statement

The break statement is used to exit from the body of the switch structure (or loop the structure).
In the switch statement, the break statement is normally used at the end of statements in each case. It exists the control from the body of the switch structure. If it is not used then the statement of other cases that come after the matching case will also be executed. Let's see how it works:

Program:

Write a program to input an integer value. Test the integer value if the value is divisible by 2, then print the message "Divisible by 2" otherwise "Not divisible by 2" by using switch statement.


#include <iostream.h>
main()
{
int n;
cout<< "Enter any value"<<endl;
cin>>n;
switch (n%2)
      {
              case 0:
                      cout <<"Divisible by 2"<<endl;
                      break;
              case 1:
                      cout  << "Not divisible by 2" <<endl;
                       break;
       }
         cout<<"OK"<<endl;
}





Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.