« Previous                                                                  Next Chapter »
Switch Case

Switch case is a multiple or multi way branching decision making statement.When we use nested if-else statement to check more than 1 condition then the complexity of a program increases in case of a lot of conditions. Thus, the program is difficult to read and maintain. So to overcome this problem, C provides 'switch case'. Switch case checks the value of a expression against a case values, if condition matches the case values then the control is transferred to that statements.

Syntax:

switch(expression)

{

  case expr1:

    statements;

    break;

  case expr2:

    statements;

    break;

     .

     . 

  case exprn:

    statements;

    break;         

  default:

    statements;

In above syntax, switch, case, break are keywords.

expr1, expr2 ...exprn are known as 'case labels.'

Statements inside case expression need not to be closed in braces.

Break statement causes an exit from switch statement.

Default case is optional case. When neither any match found, it executes.


Rules for declaring Switch case

  • The case label should be integer or character constant.
  • Each compound statement of a switch case should contain break statement to exit from case.
  • Case labels must end with (:) colon.


Advantages of Switch case

  • Easy to use.
  • Easy to find out errors.
  • Debugging is made easy in switch case.
  • Complexity of a program is minimized.

More Examples
          /* example for switch case */
#include<stdio.h>
#include<conio.h>
main()
{
int ch;
clrscr();
printf("1.one\n");
printf("2.two\n");
printf("3.three\n");
printf("enter the choice");
scanf("%d",&ch);
          switch(ch)
          {
          case 1 : printf("one");
                   break;
          case 2 : printf("two");
                   break;
          case 3 : printf("three");
                   break;
          default : printf("choose 1 or 2 or 3");
          }
}
 
          /* second logic */
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
clrscr();
printf("1.one\n");
printf("2.two\n");
printf("3.three\n");
printf("enter the choice");
scanf("%c",&ch);
          switch(ch)
          {
          case '1' : printf("one");
                   break;
          case '2' : printf("two");
                   break;
          case '3' : printf("three");
                   break;
          default  : printf("choose 1 or 2 or 3");
          }
}
 
          /* program to print the statements in colors */
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
clrscr();
printf("r.red\n");
printf("g.green\n");
printf("b.blue\n");
printf("enter the choice");
scanf("%c",&ch);
          switch(ch)
          {
          case 'r' : textcolor(RED);
                   cprintf("RED");
                   break;
          case 'g' : textcolor(GREEN);
                   cprintf("GREEN");
                   break;
          case 'b' : textcolor(BLUE);
                   cprintf("BLUE");
                   break;
          default  :  textcolor(GREEN);
                    cprintf("choose r or g or b");
          }
}
 
      /* program to arithmetic calculations by using switch case */
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
int a,b;
clrscr();
printf("1.addition\n");
printf("2.subtraction\n");
printf("3.product\n");
printf("4.division\n");
printf("5.reminder\n");
printf("enter the choice");
scanf("%c",&ch);
          switch(ch)
          {
          case '1' : printf("enter the values of a,b");
                   scanf("%d %d",&a,&b);
                   printf("the sum is %d\n",a+b);
                   break;
          case '2' : printf("enter the values of a,b");
                   scanf("%d %d",&a,&b);
                   printf("the subtraction is %d\n",a-b);
                   break;
          case '3' : printf("enter the values of a,b");
                   scanf("%d %d",&a,&b);
                   printf("the product is %d\n",a*b);
                   break;
          case '4' : printf("enter the values of a,b");
                   scanf("%d %d",&a,&b);
                   printf("the division is %f\n",(float)a/b);
                   break;
          case '5' : printf("enter the values of a,b");
                   scanf("%d %d",&a,&b);
                   printf("the reminder is %d\n",a%b);
                   break;
          default  : printf("choose 1 or 2 or 3 or 4 or 5");
          }
}


  « Previous                                                                  Next Chapter »