Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0078 switch statement

switch statement executes different branches based on the selected value. It has the following format: switch(value){ case selectedValue: statements case selecteValue: statements ... default selectedValue: statements } The following code displays the detailed message for a grading schema. using System; class Program { static void Main(string[] args) { string ch = "C"; switch (ch) { case "A": Console.WriteLine("Excellent"); break; case "B": Console.WriteLine("Good"); break; case "C": Console.WriteLine("OK"); break; case "D": Console.WriteLine("No so good"); break; case "E": Console.WriteLine("Failed"); break; } } } The output: OK You can use the string and enum value as the switch control value.