Switch statement in C and C++

Switch statement


In the previous section, I have discussed how to use conditional statement in C and C++ language where I showed only if-else statement. Now I shall show you another powerful keyword named switch statement to control program sequence in C and C++ 



How switch statement works

When you need to take a decision from multiple choices and select only one choice, you can use switch statement for that case. Say, you need to make a menu in your program. When a user fulfills a condition, he will get a result and when he fulfills another condition, he will find another result and so on. To handle this situation switch is your best friend. Switch statement works with case keyword. When a particular condition will be fulfill within switch, the corresponding case statement will be executed like making switch on or off in your electrical or electronics switch board.  



How to declare switch statement

The declaring rule or expression of switch statement is so simple which is shown below……
     switch (condition)
     {
         case condition_1:
             {
                 // the corresponding function goes here
               break;
            }
        case condition_2:
             {
                 // the corresponding function goes here
               break;
            }

         case condition_3:
             {
                 // the corresponding function goes here
               break;
            }
          default:
             {
                 // the corresponding function goes here
               break;
            }
     }

The switch keyword first takes the condition for which the case will be executed. If the corresponding case is found, the body of the case will be executed. If there is no case for the given condition, the default keyword will work and show the result. Look here I have used another keyword which is necessary named break. If you forget to use this keyword, the next case can be executed. So be careful to put it.


Example on switch statement  

Now I shall show you a so simple but interesting example on switch statement. It is a simple mathematical calculation using four basic mathematical operators. When the program will be executed, it will request to enter two integer numbers. Then it will show a menu telling which operation you want to do and ask to choice an option. When you choice an option, the corresponding calculation will be performed and show the result on the screen.
In my previous discussion I have showed how to edit and compile a program using visual studio and how to use data type in C and C++ program. So try it……..


#include<iostream>
#include<string>
using namespace std;

void main()
{
    int a,b,condition;
    float result;
    string msg;
    cout<<"Please enter an integer number :";
    cin>> a;
    cout<<"Please enter another integer number :";
    cin>> b;
    cout<< endl <<"Which operation do you want to do ? "<< endl;
    cout<<"1 = addition "<< endl <<"2 = Subtraction" << endl
            <<"3 = Multiplication "<< endl << "4 = Division"<< endl;
    cout<< "Please choose an option :";
    cin>>condition;

    switch(condition)
      {
      case 1:
            {
              result=a=b;
              msg="Addition";
              break;
            }
      case 2:
            {
              result=a-b;
              msg="Subtraction";
              break;
            }
      case 3:
            {
              result=a*b;
              msg="Multiplication";
            break;
            }
      case 4:
            {
              result=a/b;
              msg="Division";
              break;
            }
      default:
            {
              cout<<"Sorry your choice is incorrect. So try again.";
              msg="Result";
              break;
            }
      }

      cout<< endl << msg << " is " << result <<endl;
      cout<<endl;
}


    Compile and execute this program. You will find the following result that I told before.


Output 

1 comment:

  1. I know this web site offers quality dependent articles or reviews
    and extra stuff, is there any other web page which presents such stuff
    in quality?

    Take a look at my website :: bankruptcy in florida

    ReplyDelete