Case And Switch Statements In C#

Case And Switch Statements In C#

 

The switch statement to select one of many code blocks to be executed.

 

Syntax

switch(expression) 
{
  case a:
    // code block
    break;
  case b:
    // code block
    break;
  default:
    // code block
    break;
}

 

int day = 3;
switch (day) 
{
  case 1:
    Console.WriteLine("Monday");
    break;
  case 2:
    Console.WriteLine("Tuesday");
    break;
  case 3:
    Console.WriteLine("Wednesday");
    break;
  case 4:
    Console.WriteLine("Thursday");
    break;
  case 5:
    Console.WriteLine("Friday");
    break;
  case 6:
    Console.WriteLine("Saturday");
    break;
  case 7:
    Console.WriteLine("Sunday");
    break;
}
// Outputs "Saturday"

 

2021-10-03