10.21.2011

switch-case statement

In some programming situation, when there are number of choices and we want to choose only appropriate choice, in that situation C provided switch statement. Thus switch statement allows us to make a decision from the number of choices.
The switch statement also known as switch-case-default.
Since, the switch statement known for decision maker.
Syntax of switch statement:

switch(integer expression)
{
 case 1 :
   do this;
   break;
 case 2 :
   do this;
   break;
 case 3 :
   do this;
   break;
 case 4 :
   do this;
   break;
 default :
   and this;
   break;
}

Example of switch-case-default statement:

/*program to demonstration of switch statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {

  int r=4;
  switch(r)
  {
   case 1 :
     printf("\nI am in case 1");
     break;
   case 2 :
     printf("\nI am in case 2");
     break;
   case 3 :
     printf("\nI am in case 3");
     break;
   case 4 :
     printf("\nI am in case 4");
     break;
   case 5 :
     printf("\nI am in case 5");
     break;
   default :
     printf("\nI am in default");
     break;
  }
  getch();
 return 0;
 }


Output: I am in case 4

Different types of switch statement

(a) In switch statement case arrangement may be ascending order, descending order, scrambled order. So the following program should be valid and run:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int r=44;
  switch(r)
  {
   case 210 :
     printf("\nI am in case 210");
     break;
   case 10 :
     printf("\nI am in case 10");
     break;
   case 1 :
     printf("\nI am in case 1");
     break;
   case 44 :
     printf("\nI am in case 44");
     break;
   default :
     printf("\nI am in defalut");
     break;
  }
  getch();
 return 0;
 }
Output: I am in case 44

(b) C also allowed to use char values in case and switch statement as:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  char a='n';
  switch(a)
  {
    case 'm' :
      printf("\nI am in case m"); 
    case 'n' :
      printf("\nI am in case n");
    case 'o' :
      printf("\nI am in case o");
    case 'p' :
      printf("\nI am in case p");
    default :
      printf("\nI am in default");
  }
  getch();
  return 0;
 }

Output: I am in case n

Note: When above program run, 'm', 'n', 'o', 'p' are actually replaced by subsequent ASCII(American Standard Code for Information Interchange) code.

(c) C language allows us to check the value of any expression in a switch statement like as:
   switch(r+c*i)
   switch(10+20%2*r) 
   switch(r+10*c)
   switch(r>10 || c<20) 
Above all switch statement are valid.
And expression can also be used in cases provided they are constant expression.
Thus case 10+15 is correct,
and however case r+c is incorrect.

(d) The switch statement is very useful while writing menu driven program.

(e) The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch.

(f) The case keyword is followed by an integer or a character constant.

4 comments:

  1. how to write flowchart of switch case with if else?

    ReplyDelete
    Replies
    1. @Neha,
      Flowchart for switch case at:

      http://www.cprogrammingcodes.blogspot.in/2012/10/flowchart-for-switch-case.html

      Flowchart for if, if...else, nested if...else, if...else if at:

      http://www.cprogrammingcodes.blogspot.in/2012/10/if-else-statement-and-flowchart.html

      Delete
  2. break is not required after default statement.

    ReplyDelete
    Replies
    1. @Subharthi, yes it is right. It is only a ideal structure of switch-case statement.

      Delete