SWITCH CASE EXAMPLE IN C LANGUAGE

How can we simplify the code when more than two if's are required ? 
We use switch statements
Syntax:
  •    switch (expression) 
  •    { 
  •     case constant1:
  •     //codes to be executed if expression equals to constant1;
  •     .
  •     break;
  •     case constant2: 
  •    //codes to be executed if expression equals to constant2; 
  •    . 
  •    break; 
  •    .
  •    default: 
  •    //codes to be executed if expression doesn't match to any cases; 
  • }
Example:
/* Program to create a simple calculator for addition, subtraction, multiplication and division */ 
  1. # include <stdio.h>
  2. # include <conio.h>
  3. int main()
  4. {
  5. char operator; //Declaring variables
  6. float num1,num2; //Declaring variables
  7. printf("Enter operator +, - , * or / :\n"); 
  8. operator=getch();// if operator is other than +, -, * or /, error message is shown 
  9. printf("\nEnter two operands:\n"); 
  10. scanf("%f%f",&num1,&num2); //get input numbers
  11.  switch(operator) // Switches to that particular case
  12. {
  13.  case '+':
  14.  printf("num1+num2=%.2f",num1+num2); // prints calculated value
  15. break; // is used to exit from switch case 
  16.  case '-': 
  17. printf("num1-num2=%.2f",num1-num2); 
  18. break; 
  19. case '*': 
  20. printf("num1*num2=%.2f",num1*num2); 
  21. break; 
  22. case '/': printf("num2/num1=%.2f",num1/num2);
  23.  break;
  24.  default: // If no case is executed then by default this case is executed
  25. printf(Error! operator is not correct"); 
  26. break;
  27.  }
  28.  return 0; 
  29. }

Output :
Enter operator +, -, * or / :
 /
 Enter two operators: 
12
5
num2/num1=2.4;

Description:
This example shows how to use switch case.
Switch can be user when we need two or more if conditions.
Line 1,2 - Here we include two header files.
Line 5,6 - Declares variables.
Line 8 - Gets a character input from console. Important thing is to be noted that if anything except +,-,*,/ is typed  then console returns error.


Actual working of switch case:
Line 11 -  switch(operator): operator is a variable in which we have already taken input.
Now what "switch" does is it switches to particular case character as present in "operator" variable. Suppose operator has value + (plus) then switch() will be switched to first case.
Similarly others work.


Description in marathi: рдорд░рд╛рдареАрдд рд╡рд░реНрдгрди:
рд╣реНрдпрд╛ рдЙрдзрд╛рд░реНрдирд╛рде рд╕реНрд╡реАрдЪ рдХреЗрд╕ рдХрд╕рд╛ рд╡рд╛рдкрд░рддрд╛рдд рддреЗ рджрд┐рд▓реЗрд▓рд╛ рдЖрд╣реЗ. рд╕реНрд╡реАрдЪ рдХреЗрд╕ рдЖрдкрдг рд╡рд╛рдкрд░реВ рд╢рдХрддреЛ рдЬреЗрд╡реНрд╣рд╛ реи рдкреЗрдХреНрд╖рд╛ рдЬрд╛рд╕реНрдд " рдЗрдл рдПрд▓реНрд╕ " рд╡рд╛рдкрд░рд╛рдпрдЪреЗ рдЕрд╕реЗрд▓ .
рдУрд│ рез,реи - рдЗрдереЗ рдЖрдкрдг рджреЛрди рд╣реЗрдбрд░ реЮрд╛рдЗрд▓ рд╡рд╛рдкрд░рдгреНрдпрд╛ рдХрд░рд┐рддрд╛ рддреА рдУрд│ рд▓рд┐рд╣рд┐рд▓реЗрд▓реА рдЖрд╣реЗ.
рдУрд│ рел,рем - рдкреНрд░реЛрдЧреНрд░рд╛рдо рдордзреНрдпреЗ рдЬреЗ рд╡реЗрд░рд┐рдПрдмреНрд▓ рд╡рд╛рдкрд░рд▓реЗ рдЬрд╛рдгрд╛рд░ рддреЗ рдЗрдереЗ рд▓рд┐рд╣рд┐рд▓реЗрд▓реЗ рдЖрд╣реЗ.
рдУрд│ рео - рдХрдиреНрд╕реЛрд▓ рд╡рд░реВрди рез рдХрд╛рд░рд╛рдХреНрдЯреЗрд░ рдШреЗрдгреНрдпрд╛рд╕рд╛рдареА рд╣рд┐ рдУрд│ рд▓рд┐рд╣рд┐рд▓реЗрд▓реЗ рдЖрд╣реЗ.рддреЛ рдУрдкреНрд░реЗрддрд░  рдирд╛рд╡рд╛рдЪрд╛ рд╡рд░рд┐рдЕрдмреНрд▓ рдордзреНрдпреЗ рдЬрдорд╛ рд╣реЛрддреЛ.рдЗрдереЗ "getch" рд╡рд╛рдкрд░рддрд╛рдВрдирд╛ рдЬрд░ +-*/ рд╣реНрдпрд╛ рдкреЗрдХреНрд╖рд╛ рджреБрд╕рд░рд╛ рдХрд╛рд╣реА рдХрдиреНрд╕реЛрд▓рд╡рд░ рд▓рд┐рд╣рд┐рд▓реЗ рддрд░ рдХрдиреНрд╕реЛрд▓рд╡рд░ рдПрд░реНрд░реЛрд░ рджрд┐рд╕реЗрд▓.             
 
рд╕реНрд╡реАрдЪ  рдХреЗрд╕ рдкреНрд░рддреНрдпрдХреНрд╖рд╛рдд рдХрд╕рд╛ рдХрд╛рдо рдХрд░рддреЛ ?:
рдУрд│ резрез - Switch(operator) :рдУрдкреНрд░реЗрддрд░ рд╣рд╛ рдПрдХ рдЕрд╕рд╛ рд╡рд░рд┐рдЕрдмреНрд▓ рдЖрд╣реЗ рдЬреНрдпрд╛рдд рдЖрдкрдг рдЖрдзреАрдЪ рдпреБрд╕рд░ рдХрдбреВрди рдХрд╛рд░рд╛рдХреНрдЯрд╛рд░ рдШреЗрддрд▓рд╛ рдЖрд╣реЗ.рдЖрддрд╛ рдУрдкреНрд░реЗрддрд░ рд╡рд░рд┐рдЕрдмреНрд▓  рдордзреНрдпреЗ рдЬреЗ рдХрд╛рд╣реА рдЖрд╣реЗ рддреЛ рдХреЗрд╕ рдордзрд▓рд╛ рдХреЛрдб рд░рди рд╣реЛрддреЛ. рдЬрд░ рдУрдкреНрд░реЗрддрд░ рд╡рд░рд┐рдЕрдмреНрд▓ рдордзреНрдпреЗ ' + ' рдЕрд╕реЗрд▓ рддрд░ рдкрд╣рд┐рд▓рд╛ рдХреЗрд╕ рд░рди рд╣реЛрдгрд╛рд░.рдЬрд░ ' - ' рдЕрд╕реЗрд▓ рддрд░ рджреБрд╕рд░рд╛.   

Comments

Popular Posts