Page 47 - Modular_V1.1_Flipbook
P. 47
break;
case 'o':
cout<<"Entered character is vowel";
break;
case 'U':
cout<<"Entered character is vowel";
break;
case 'u':
cout<<"Entered character is vowel";
break;
Output 1:
default:
Enter a character: A
cout<<"Entered character is consonant";
Entered character is vowel
}
Output 2:
getch(); Enter a character: M
} Entered character is consonant
Clickipedia
The expression of switch statement must be of integer or character type. Floating-point
constants are not allowed.
DIFFERENCE BETWEEN IF AND SWITCH STATEMENTS
Following are some differences between if and switch statements:
if statement can test any type of expressions (integer, floating-point, logical, relational, etc),
while switch can test only integer expressions.
if statement encloses multiple statements inside curly braces { }. This is shown below:
if (condition)
{
statement1;
statement2;
}
switch statement never encloses multiple statements inside curly braces { }. This is shown
below:
switch (value)
{
case 1 :
statement1;
statement2;
case 2:
. . .
}
Conditional Statements 45

