Tuesday 12 February 2013

The switch - case statement in C Programming Language


The earlier problem of determining  if the input character is a vowel can be effectively solved by using the switch - case statement. The switch - case construct is used in situations where based on the value of a variable certain action is to be taken.

#include<stdio.h>
main()
{
char in_chr;
puts("Enter a character in lower case:");
in_chr = getchar();
fflush (stdin);
switch(in_chr)
{
case 'a' : puts("A");
break;
case 'e' : puts("E");
break;
case 'i' : puts("I");
break;
case 'o' : puts("O");
break;
case 'u' : puts("U");
break;
default : puts("The Character is not a vowel");
}
}

In this function, switch() specifies the variable name whose value has to be compared with the specifed cases. The variable may be an integer expression or a character. The case statements specify values which may be integer or character constants. They also specify the corresponding action to be taken if the value compares with the value of the switch() variable. After the action for a particular case the value is specified, the break statement is used to bring the control out of the switch() block of statements.

0 comments:

Post a Comment

Powered by Blogger.