Friday 22 September 2017

Selection Statement- the switch case statement

THE SWITCH CASE STATEMENT:-
A switch statement is used for multiple-way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type.

syntax:
switch (expression)
{
  case value1:
    code segment1;
    break;
  case value2:
    code segment2;
    break;
.
.
.
  case valueN:
    code segmentN;
    break;
  default:
    default code segment;
}
Above is the representation of the switch case. In switch-case, the program asks you about your choice and that choice will run. now understand its syntax. First, we provide the choice to the switch case and that particular choice will run after that program shows the result, it doesn't go to another part of the program because that particular case terminated by the break. The default choice is there if anyone tries to run the wrong choice it will show the default code segment.

program for basic calculation by using switch case:
#include<iostream>
using namespace std;
int main(){
 int ch,x,y,z;
 cout<<"1.ADDITION"<<endl;
 cout<<"2.SUBTRACTION"<<endl;
 cout<<"3.MULTIPLY"<<endl;
 cout<<"4.DIVISION"<<endl;
 cout<<"5.MODULUS"<<endl;
 cout<<"Enter your choice:";
 cin>>ch;
 switch(ch){
  case 1:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x+y;
   cout<<"Addition of two no. is "<<z<<endl;
   break;
  case 2:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x-y;
   cout<<"Subtraction of two no. is "<<z<<endl;
   break;
  case 3: 
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x*y;
   cout<<"Multiplication of two no. is"<<z<<endl;
   break;
  case 4:
      cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x/y;
   cout<<"division of two no. is "<<z<<endl;
   break;
  case 5:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x%y;
   cout<<"Modulus of two no. is "<<z<<endl;
   break;
  default:
  cout<<"Wrong choice";
 }
 return 0;
}

output:
1.ADDITION
2.SUBTRACTION
3.MULTIPLY
4.DIVISION
5.MODULUS
Enter your choice:1
Enter first no:12
Enter second no:12
Addition of two no. is=24
In the above program, there are five choices if someone wants to choose a particular function just input the choice number and that particular function will run. The break will separate the function if you are not using the break after the break all functions will run. The default will show the wrong choice here.
Note: if you don't know what is the modulus? Please click here

Difference between if-else and switch case:

  • An if-else decision is taken on basis of the input wherein the switch-case decision is taken by the user.
  • if-else is more complex for the lengthy condition than switch-case.
  • if-else can check multiple conditions at a time where switch-case check the only single condition at a time








2 comments:

  1. In the 'differences' above, the last statement is untrue.
    Nested switch-case statements are allowed.

    ReplyDelete