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("
I am in case 1");

     break;
   case 2 :
     printf("
I am in case 2");

     break;
   case 3 :
     printf("
I am in case 3"
);

     break;
   case 4 :
     printf("
I am in case 4"
);

     break;
   case 5 :
     printf("
I am in case 5"
);

     break;
   default :
     printf("
I 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("
I am in case 210");
     break;
   case 10 :
     printf("
I am in case 10");
     break;
   case 1 :
     printf("
I am in case 1");
     break;
   case 44 :
     printf("
I am in case 44");
     break;
   default :
     printf("
I 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("
I am in case m"
);
 
    case n :
      printf("
I am in case n"
);
    case o :
      printf("
I am in case o"
);
    case p :
      printf("
I am in case p"
);
    default :
      printf("
I 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.

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.