switch (“Expression”)
{
Case Expr 1:
{
Statements
.
.
break;
}
Case Expr 2:
{
Statements
.
.
break;
}
.
.
.
Default:
{
Statement
.
.
.
}
}
A program is very
difficult to understand when there are too many if statements representing
multiple conditional statements. To avoid using multiple statements, in certain
cases the switch... case approach can be used as an alternative.
In switch case, depend
on condition particular case will get execute and break keyword lets the switch
block stops when the desire value is met. The default keyword lets the program
execute the statements that follow it when the desire value is not met.
Example
:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CABasics
{
class Switch9
{
public static
void Main()
{
Console.WriteLine("Enter
an alphabet");
string s = Console.ReadLine();
switch (s)
{
case "a":
case "e":
case "i":
case "o":
case "u":
Console.WriteLine("You
Have Entered an Vowel");
break;
default:
Console.WriteLine("Not
a vowel");
break;
}
Console.ReadLine();
}
}
}
|
No comments:
Post a Comment