Switch

Pick one of many blocks of code by the value of a single variable with C#'s switch statement, including shared cases and a default.

Apprentice · C# · Updated

After this lesson you can

  • Choose between many values of one variable with a switch statement

Key takeaways

  • A switch checks one value against a list of exact values and runs the code under the case that matches.
  • In C#, every case ends with break (or return), so code never runs on into the next case.
  • Stack case labels with nothing between them to share one block. default runs when no case matches.
  • Use a switch for exact values of one variable; use if and else if for ranges or several variables.

When one variable can hold many values, and each value needs its own code, a long chain of else if gets hard to read. A switch statement says the same thing more clearly: here's the value, here's what to do for each case.

The shape of a switch

switch (variable)
{
    case value1:
        // runs when variable == value1
        break;
    case value2:
        // runs when variable == value2
        break;
    default:
        // runs when no case matches
        break;
}
  • The value in parentheses is checked once.
  • Each case names one possible value, followed by a colon and the code to run.
  • break ends the case. In C#, every case must end with break (or return), so code never runs on into the next case by accident.
  • default is optional, and runs when no case matches.

An example

Based on the day of the week, suggest where to eat:

string dayOfTheWeek = "Wednesday";

switch (dayOfTheWeek)
{
    case "Monday":
        Console.WriteLine("Soup and sandwiches at the corner deli.");
        break;
    case "Tuesday":
        Console.WriteLine("Taco Tuesday!");
        break;
    case "Wednesday":
        Console.WriteLine("Mid-week sushi.");
        break;
    case "Thursday":
        Console.WriteLine("Pasta night.");
        break;
    case "Friday":
        Console.WriteLine("Burgers to start the weekend.");
        break;
    default:
        Console.WriteLine("It's the weekend! Try somewhere new.");
        break;
}
Mid-week sushi.

Several values, one block

To run the same code for more than one value, stack the case labels with nothing between them:

string dayOfTheWeek = "Sunday";

switch (dayOfTheWeek)
{
    case "Saturday":
    case "Sunday":
        Console.WriteLine("Sleep in.");
        break;
    default:
        Console.WriteLine("Set an alarm.");
        break;
}
Sleep in.

switch or if?

A switch compares one value against a list of exact values. Use if and else if when you compare ranges (availableCash > 50) or several different variables. Both make the same decisions; choose the one that reads more clearly.

Check yourself

2 questions to try on the lesson page.

So what

A switch lays out a many-way decision as a list you can read at a glance, and C#'s rule that every case ends with break keeps one case from spilling into the next.

Key terms

Cheat sheets