C# Switch Case Statement Tutorial with Examples

When writing programs, you’ll often need to make decisions based on different conditions. For example:

  • If a user enters 1, display “Home.”
  • If they enter 2, display “Settings.”
  • If they enter 3, display “Profile.”

While you can solve this using multiple if-else statements, C# provides a cleaner and more readable alternative—the switch statement.

The switch statement allows your program to compare a single expression against multiple possible values and execute the matching block of code.

In this guide, you’ll learn everything you need to know about the C# switch statement, including:

  • What a switch statement is
  • Syntax and flow
  • How case works
  • The default case
  • Using multiple cases
  • Grouping cases
  • break statement
  • goto case
  • goto default
  • Nested switches
  • Switch expressions (modern C#)
  • Switch vs if-else
  • Best practices
  • Common mistakes
  • Real-world examples

What Is a Switch Statement?

A switch statement is a selection statement that executes one block of code from many possible choices.

Instead of writing several if-else if conditions, you can write cleaner code using switch.

Think of it like choosing from a menu.

Choose an option

1Home
2Search
3Settings
4Exit

Depending on the selected number, a different block of code runs.


Syntax

switch (expression)
{
    case value1:
        // Code
        break;

    case value2:
        // Code
        break;

    default:
        // Code if no match
        break;
}

Understanding the Parts

expression

This is the value being tested.

int day = 3;

switch (day)

The switch compares day against each case.


case

Each case represents a possible value.

case 1:

If the expression equals 1, the code inside executes.


break

Stops the switch statement.

case 2:
    Console.WriteLine("Tuesday");
    break;

Without break, execution would continue into the next case (C# generally prevents accidental fall-through unless you explicitly use goto).


default

Runs when none of the cases match.

default:
    Console.WriteLine("Invalid option");
    break;

The default case is optional but recommended.


First Example

using System;

class Program
{
    static void Main()
    {
        int number = 2;

        switch (number)
        {
            case 1:
                Console.WriteLine("One");
                break;

            case 2:
                Console.WriteLine("Two");
                break;

            case 3:
                Console.WriteLine("Three");
                break;

            default:
                Console.WriteLine("Unknown Number");
                break;
        }
    }
}

Output

Two

How Switch Works

Suppose:

int grade = 3;

Execution happens like this:

Start


switch(grade)

   ├── case 1 ❌
   ├── case 2 ❌
   ├── case 3 ✅
   │      │
   │      ▼
   │  Execute code
   │      │
   │      ▼
   │    break

End

Example with Strings

A switch statement can also work with strings.

string fruit = "Apple";

switch (fruit)
{
    case "Apple":
        Console.WriteLine("Red fruit");
        break;

    case "Banana":
        Console.WriteLine("Yellow fruit");
        break;

    case "Orange":
        Console.WriteLine("Orange fruit");
        break;

    default:
        Console.WriteLine("Unknown fruit");
        break;
}

Output

Red fruit

Example with Characters

char grade = 'A';

switch (grade)
{
    case 'A':
        Console.WriteLine("Excellent");
        break;

    case 'B':
        Console.WriteLine("Good");
        break;

    case 'C':
        Console.WriteLine("Average");
        break;

    default:
        Console.WriteLine("Fail");
        break;
}

Grouping Multiple Cases

Sometimes several cases should execute the same code.

Example:

char vowel = 'e';

switch (vowel)
{
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        Console.WriteLine("Vowel");
        break;

    default:
        Console.WriteLine("Consonant");
        break;
}

Output

Vowel

Here, five cases share the same code block.


Example: Days of the Week

int day = 6;

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;

    default:
        Console.WriteLine("Invalid Day");
        break;
}

Example: Calculator

int a = 10;
int b = 5;
char op = '*';

switch (op)
{
    case '+':
        Console.WriteLine(a + b);
        break;

    case '-':
        Console.WriteLine(a - b);
        break;

    case '*':
        Console.WriteLine(a * b);
        break;

    case '/':
        Console.WriteLine(a / b);
        break;

    default:
        Console.WriteLine("Invalid Operator");
        break;
}

Output

50

Nested Switch Statements

A switch can be placed inside another switch.

string department = "IT";
int level = 2;

switch (department)
{
    case "IT":
        switch (level)
        {
            case 1:
                Console.WriteLine("Junior Developer");
                break;

            case 2:
                Console.WriteLine("Senior Developer");
                break;
        }
        break;

    case "HR":
        Console.WriteLine("Human Resources");
        break;
}

The default Case

The default block runs if no case matches.

int month = 15;

switch (month)
{
    case 1:
        Console.WriteLine("January");
        break;

    default:
        Console.WriteLine("Invalid Month");
        break;
}

Output

Invalid Month

Understanding break

Each case normally ends with break.

case 1:
    Console.WriteLine("One");
    break;

The break statement exits the switch immediately.

Without it, C# does not allow execution to continue into the next case unless the case is intentionally empty or you explicitly transfer control using goto.


Using goto case

The goto case statement transfers execution directly to another case inside the same switch statement.

Example:

int number = 1;

switch (number)
{
    case 1:
        Console.WriteLine("One");
        goto case 2;

    case 2:
        Console.WriteLine("Two");
        break;

    default:
        Console.WriteLine("Other");
        break;
}

Output

One
Two

Execution flow:

case 1


Print One

goto case 2


Print Two

 break

When should you use goto case?

It is useful when multiple cases share additional logic but you want to avoid duplicating code.

Example:

int score = 100;

switch (score)
{
    case 100:
        Console.WriteLine("Perfect Score!");
        goto case 90;

    case 90:
        Console.WriteLine("Grade A");
        break;

    default:
        Console.WriteLine("Keep Practicing");
        break;
}

Output

Perfect Score!
Grade A

Using goto default

You can also transfer control to the default case.

int choice = 1;

switch (choice)
{
    case 1:
        Console.WriteLine("Menu");
        goto default;

    default:
        Console.WriteLine("Returning to Main Menu");
        break;
}

Output

Menu
Returning to Main Menu

Switch Expressions (Modern C#)

Starting with modern versions of C#, you can use a switch expression, which is a shorter, expression-based alternative to the traditional switch statement.

Example:

int day = 3;

string dayName = day switch
{
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    4 => "Thursday",
    5 => "Friday",
    6 => "Saturday",
    7 => "Sunday",
    _ => "Invalid Day"
};

Console.WriteLine(dayName);

Output

Wednesday

Notice that:

  • => replaces the traditional case syntax.
  • _ acts like the default case.
  • No break statements are required.
  • The switch expression returns a value.

Switch Statement vs If-Else

Both are used for decision-making, but they serve different purposes.

FeatureSwitchIf-Else
Best forOne variable with many specific valuesComplex conditions
ReadabilityExcellentCan become lengthy
Supports rangesNo (traditional switch)Yes
Multiple conditionsLimitedExcellent
PerformanceOften optimized by the compiler for constant valuesDepends on the conditions

Example using if-else

if (choice == 1)
{
    Console.WriteLine("Home");
}
else if (choice == 2)
{
    Console.WriteLine("Settings");
}
else if (choice == 3)
{
    Console.WriteLine("Profile");
}
else
{
    Console.WriteLine("Invalid");
}

Same logic using switch

switch (choice)
{
    case 1:
        Console.WriteLine("Home");
        break;

    case 2:
        Console.WriteLine("Settings");
        break;

    case 3:
        Console.WriteLine("Profile");
        break;

    default:
        Console.WriteLine("Invalid");
        break;
}

The switch version is shorter, easier to scan, and simpler to maintain when comparing one expression against many constant values.

When to use if-else

Choose if-else when:

  • Comparing ranges (score >= 90)
  • Combining multiple conditions with && or ||
  • Comparing different variables
  • Performing complex logical expressions

Example:

if (age >= 18 && hasLicense)
{
    Console.WriteLine("Can drive");
}

A traditional switch statement is not suitable for this type of logic.


Common Mistakes

1. Forgetting break

case 1:
    Console.WriteLine("One");

This causes a compile-time error because the end of the case is reachable. Add break, return, throw, or another jump statement.

Correct:

case 1:
    Console.WriteLine("One");
    break;

2. Duplicate Case Values

Incorrect:

case 1:
    break;

case 1:
    break;

Each case value must be unique.


3. Using Variables as Case Labels

Incorrect:

int x = 5;

case x:

Case labels must be compile-time constants.


Best Practices

  • Use switch when comparing one expression against many fixed values.
  • Keep each case short and focused.
  • Group cases that perform the same action.
  • Include a default case whenever practical.
  • Use meaningful case values.
  • Use goto case sparingly, only when it improves clarity.
  • Consider switch expressions for simple value-to-value mappings in modern C#.

Real-World Example: ATM Menu

Console.WriteLine("1. Deposit");
Console.WriteLine("2. Withdraw");
Console.WriteLine("3. Balance");

int option = Convert.ToInt32(Console.ReadLine());

switch (option)
{
    case 1:
        Console.WriteLine("Deposit Selected");
        break;

    case 2:
        Console.WriteLine("Withdraw Selected");
        break;

    case 3:
        Console.WriteLine("Balance Selected");
        break;

    default:
        Console.WriteLine("Invalid Option");
        break;
}

Real-World Example: Traffic Lights

string light = "Green";

switch (light)
{
    case "Red":
        Console.WriteLine("Stop");
        break;

    case "Yellow":
        Console.WriteLine("Get Ready");
        break;

    case "Green":
        Console.WriteLine("Go");
        break;

    default:
        Console.WriteLine("Signal Error");
        break;
}

Summary

The C# switch statement is an excellent choice when your program needs to select one action from several predefined options based on a single expression. Compared to long chains of if-else if statements, it usually produces code that is easier to read, maintain, and extend.

Key points to remember:

  • A switch evaluates one expression and compares it with multiple case labels.
  • Each case should typically end with a break statement to exit the switch.
  • The default case handles values that do not match any defined case.
  • Multiple cases can share the same block of code by grouping them together.
  • goto case and goto default allow explicit control transfer between cases, but they should be used sparingly to keep code readable.
  • Modern C# also provides switch expressions, which offer a concise way to map values and return results.
  • Use switch for selecting among fixed constant values, and use if-else when working with ranges, multiple variables, or more complex logical conditions.

Mastering the switch statement will help you write cleaner, more organized C# programs and make your decision-making logic easier to understand.