1. Introduction
Imagine you're a barista at a popular coffee shop. A customer comes in and names one of their five favorite drinks. You don't start a philosophical debate, you just glance at the menu and react: "Cappuccino? Of course! Here's your cappuccino!" If you checked each item with a separate if, your code would look like a stack of cups after the lunch rush—bulky and not very convenient.
The switch statement lets you elegantly check a variable's value and do different things depending on the result. It only works with certain data types (more on that in a sec), but for most user menus, simple commands, or status recognition, it's the perfect tool.
2. Classic switch Syntax in C#
Here's a minimal flowchart of how it works:
switch (Expression) {
case Value1:
// actions
break;
case Value2:
// actions
break;
...
default:
// default actions
break;
}
Example 1: classic menu
Console.WriteLine("Pick a drink: 1 - Coffee, 2 - Tea, 3 - Cocoa");
string input = Console.ReadLine();
int choice = int.Parse(input);
switch (choice)
{
case 1:
Console.WriteLine("You picked coffee. Great choice!");
break;
case 2:
Console.WriteLine("Tea is good too.");
break;
case 3:
Console.WriteLine("Cocoa is for the cozy crowd!");
break;
default:
Console.WriteLine("That drink isn't on the menu.");
break;
}
- The switch block checks choice for equality with the values after case.
- After each option, you gotta write break; — otherwise, execution will fall through to the next case (more on that below).
- The default block is like else; it runs if none of the cases match.
How does switch work inside? — Step by step
- First, the expression in parentheses after switch is evaluated (in our example, the choice variable).
- The program compares it to each specified value (case) in order.
- If it matches — the instructions in that block run. Then you hit break, and the program exits the switch block.
- If none of the cases match, the default block runs (if it's there).
Visual diagram
| Variable value | case 1 | case 2 | case 3 | default |
|---|---|---|---|---|
| 1 | + | |||
| 2 | + | |||
| 3 | + | |||
| other | + |
“+” — this block runs
3. What types does classic switch support?
In classic syntax (so, without "Pattern Matching" and the new stuff we'll talk about later!), switch supports:
- Simple integer types (int, byte, char, long, short, sbyte, ushort, uint, ulong)
- Strings (string)
- Enums (enum)
- Characters (char)
Example 2: switching on strings
Console.WriteLine("Pick a command: start, stop, pause");
string command = Console.ReadLine();
switch (command)
{
case "start":
Console.WriteLine("Program started!");
break;
case "stop":
Console.WriteLine("Program stopped.");
break;
case "pause":
Console.WriteLine("Program is paused.");
break;
default:
Console.WriteLine("Unknown command.");
break;
}
4. Why do you need break and what happens if you forget it?
The most popular newbie programmer glitch is forgetting break; inside a case. Let's see what happens if you skip it.
Example 3: falling into the next block (fall-through)
int day = 2;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Unknown day");
break;
}
What happens?
- Input is day = 2.
- First, the line runs: "Tuesday"
- No break — execution immediately falls into case 3 and runs: "Wednesday"
- Then you hit break; — exit.
On the screen you'll see:
Tuesday
Wednesday
This is a classic mistake!
- If you want to do ONE thing, put break; in every case.
- If (rarely) you want some options to do the same thing, you can "glue" them together (see below).
5. Grouping case values
Sometimes several values should behave the same way. Why copy-paste the same code?
Example 4: same reaction for several cases
char grade = 'B';
switch (grade)
{
case 'A':
case 'B':
case 'C':
Console.WriteLine("Passed!");
break;
case 'D':
case 'E':
Console.WriteLine("Room to improve.");
break;
default:
Console.WriteLine("Invalid grade.");
break;
}
What's happening?
- If grade is 'A', 'B' or 'C', the first block runs: "Passed!".
- If 'D' or 'E' — the second block.
- Anything else — default.
6. What you can and can't do in classic switch
You can:
- Use literals (constants) in case, like: case 1:, case 'A':, case "stop":.
- Compare enum (enums).
- Skip default, but it's usually helpful.
You can't:
- Use variables in case (only constants).
- Compare by ranges (case >= 5:, case x > 10: — nope!).
- Use double, float and other floating-point values — only integers, string, char, enum.
- Write several values comma-separated in one case (case 1, 2: — don't do that; write several cases in a row).
7. Features and common mistakes
In real projects, switch is often used for menus, handling statuses, commands, error codes, and a thousand other things. But, like any tool, it's got its quirks.
Common mistakes:
- Missing break (already talked about it).
- Wrong type for the switch expression (like trying to use double).
- Case-sensitive string mismatch (case "Start": won't match "start") — string comparison is case-sensitive!
- Unprocessed value (forgot default, user entered something weird).
Trap with nested switches
You might see code like that, but most of the time it's hard to read. If you catch yourself nesting switches, maybe it's time to move your code to a separate method or even use OOP/enum/array.
GO TO FULL VERSION