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
switchchecks one value against a list of exact values and runs the code under thecasethat matches. - In C#, every case ends with
break(orreturn), so code never runs on into the next case. - Stack
caselabels with nothing between them to share one block.defaultruns when no case matches. - Use a
switchfor exact values of one variable; useifandelse iffor 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
casenames one possible value, followed by a colon and the code to run. breakends the case. In C#, every case must end withbreak(orreturn), so code never runs on into the next case by accident.defaultis 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.
Do it: name the deli's soup of the day
The corner deli makes one soup each weekday and none on the weekend. Its sign needs the soup for any day.
The method gets one value:
dayOfTheWeek: the day's name, astringwith a capital first letter ("Tuesday").
The soups:
| Day | Soup |
|---|---|
"Monday" |
"Tomato" |
"Tuesday" |
"Chicken noodle" |
"Wednesday" |
"Minestrone" |
"Thursday" |
"Chicken noodle" |
"Friday" |
"Clam chowder" |
| any other day | "No soup today" |
For example, "Tuesday" matches the Tuesday case, so SoupOfTheDay("Tuesday") returns "Chicken noodle".
Your job: write the method string SoupOfTheDay(string dayOfTheWeek) with a switch statement.
- Return the day's soup from the table.
- Tuesday and Thursday have the same soup: give them one shared block.
- Any other value, such as
"Saturday"or"Sunday", returns"No soup today".
Practiced live on the lesson page: write the C# right there and check it.
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.