switch..case..default

The switch statement evaluates an expression against a condition or multiple conditions and executes a labeled code block. These labeled code blocks are called switch labels. Each switch label is followed by a break statement which helps the program come out of the loop and move on to the next statement. In the preceding example, where we checked for vowels using the if...else statement, we used if...else for each vowel and a default value for any other character. This can be further simplified using a switch...case...default statement.

All we want is to have a condition expression check the character. If it matches any of the matching expressions, that is, a vowel, it prints the vowel; otherwise, it prints that it is not a vowel:

Console.Write("Enter a character: ");
char ch1 = (char)Console.Read();
switch (ch1)
{
case 'a' :
case 'e':
case 'i' :
case 'o' :
case 'u':
Console.WriteLine("The character entered is a vowel and it is: " + ch1);
break;
default:
Console.WriteLine("The character entered is not vowel and it is: " + ch1);
break;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.68.159