<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Control flow
All lessons in this course

Lesson 8 of 60 11 minBeginner

switch statements and expressions

Pick one of many cases cleanly with switch, use the modern switch expression, and match ranges with patterns.

When one value can match many exact options (a menu choice, a day number, a status code), a long else if chain gets hard to read. switch compares one value against a list of cases.

Interactive

Which case matches?

Choose a day number. switch jumps straight to the matching case. 0 and 8 match no case, so default runs.

Day.cs
int day = 3;string name; switch (day){    case 1: name = "Monday"; break;    case 2: name = "Tuesday"; break;    case 3: name = "Wednesday"; break;    case 4: name = "Thursday"; break;    case 5: name = "Friday"; break;    case 6: name = "Saturday"; break;    case 7: name = "Sunday"; break;    default: name = "Invalid day"; break;}Console.WriteLine(name);
Output
Wednesday
case 1Monday
case 2Tuesday
case 3Wednesday
case 4Thursday
case 5Friday
case 6Saturday
case 7Sunday
defaultInvalid day

The switch statement

Menu.cs
Console.Write("Choose 1-3: ");string? choice = Console.ReadLine(); switch (choice){    case "1":        Console.WriteLine("New project");        break;    case "2":        Console.WriteLine("Open project");        break;    case "3":        Console.WriteLine("Exit");        break;    default:        Console.WriteLine("Unknown option");        break;}
Output
Choose 1-3: 2
Open project
  • Each case ends with break (or return / throw). C# does not let one case “fall through” into the next by accident.
  • default runs when no case matches. It is optional, but almost always a good idea.
  • You can switch on int, string, char, enum, bool and more.

Several cases, one action

Program.cs
char grade = 'B';switch (grade){    case 'A':    case 'B':        Console.WriteLine("Great work");        break;    case 'C':        Console.WriteLine("Good");        break;    default:        Console.WriteLine("Keep practising");        break;}
Output
Great work

The switch expression (C# 8+)

When each case just produces a value, the switch expression is much shorter. Each arm is pattern => result, arms are separated by commas, and _ is the “anything else” arm.

Program.cs
int day = 6;string type = day switch{    6 or 7 => "Weekend",    >= 1 and <= 5 => "Weekday",    _ => "Invalid"};Console.WriteLine(type);
Output
Weekend

Patterns like >= 1 and <= 5 (relational patterns) and 6 or 7 let one arm match a range or several values.

Shipping.cs
decimal weightKg = 3.2m;decimal cost = weightKg switch{    <= 1m => 50m,    <= 5m => 120m,    <= 20m => 300m,    _ => 800m};Console.WriteLine($"Shipping: Rs {cost}");
Output
Shipping: Rs 120
Quick check

In a switch expression, what does the _ arm mean?

Quick check

What happens if you leave out break at the end of a non-empty case in C#?

Practice

Write a switch expression that converts a month number (1–12) into its number of days in a non-leap year. Return -1 for invalid input.

Show a hint

Group months: 2 => 28, 4 or 6 or 9 or 11 => 30, >= 1 and <= 12 => 31.

Show the solution
Solution.cs
int month = 9;int days = month switch{    2 => 28,    4 or 6 or 9 or 11 => 30,    >= 1 and <= 12 => 31,    _ => -1};Console.WriteLine(days);
Output
30

Remember

  • switch compares one value to many cases; default catches the rest.
  • Every case section ends with break (no accidental fall-through).
  • Switch expressions (x switch { ... }) return a value and are concise.
  • Patterns: or, and, <, >= and _ (anything).
0 of 60 lessons completed