Monday, 17 September 2012

Switch statement


Switch statement is alternative of nested if-else.it is executed when there are many choices and only one is to be executed.
Syntax:
switch(expression)
{ 
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;

default:
statement;
}
Flow chart
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char c;
clrscr();
printf("Enter an alphabet:");
scanf("%c",&c);
switch(c)
{
 case'a':
 case'A':
 printf("You entered vowel.");
 break;
 case'e':
 case'E':
 printf("You You entered vowel.");
 break;
 case'i':
 case'I':
 printf("You entered vowel.");
 break;
 case'o':
 case'O':
 printf("You entered vowel.");
 break;
 case'u':
 case:'U'
 printf("You entered vowel.");
 break;
 default:
 printf("You entered a consonant.");
}
getch();
}


No comments:

Post a Comment