Ads 468x60px

Labels

Thursday, 13 September 2012

Switch Case control structure


SWITCH stmt is used for handling n number of decisions at a time.
 Hence it is also known as multi way decision making stmt.
SWITCH STATEMENT can't be applied to float and double.
 Break stmt transfers the control out of the switch stmt.
Syntax

switch(choice)
  {
  case 1:block of stmt:-1;
  break;
  case 2:block of stmt:-2;
  break;
  …………………….
  ……………………..
  ………………………
  case n:    block of stmt:-n;
  break;
  default: invalid stmt;
  break;
  }


sample program 
#include<stdio.h>
#include<conio.h>
void main( )
{
  char ch;
  clrscr();
  printf ("Enter  any character \n " ) ;
  scanf ( "%d", &ch) ;
  switch(ch)
  {
  case ‘a’:printf(“it is vowel\n”);
  break;
  case ‘e’:printf(“it is vowel\n”);
  break;
  case ‘i’:printf(“it is vowel\n”);
  break;
  case ‘o’:printf(“it is vowel\n”);
  break;
  case ‘u’:printf(“it is vowel\n”);
  break;
  default:printf(“\n it is constant”);
  }
  getch();
}