C# Switch Statement

Welcome to The Coding College! In this tutorial, we’ll learn about the switch statement in C#, one of the most useful tools for handling multiple conditions in your program.

By the end of this guide, you will:

  • Understand what the switch statement is.
  • Learn its syntax and how it works.
  • Explore practical examples to implement it efficiently.

Let’s get started! 🚀

What is the Switch Statement in C#?

The switch statement in C# is a control flow statement used to execute one block of code among multiple options, based on the value of an expression. It is a cleaner alternative to writing multiple if...else conditions when checking a single variable against multiple values.

Syntax of Switch Statement

Here is the basic syntax of a switch statement in C#:

switch (expression)  
{  
    case value1:  
        // Code to execute if expression == value1  
        break;  

    case value2:  
        // Code to execute if expression == value2  
        break;  

    case value3:  
        // Code to execute if expression == value3  
        break;  

    default:  
        // Code to execute if no case matches  
        break;  
}

How It Works:

  1. The expression is evaluated once.
  2. The value of the expression is compared with each case.
  3. If a match is found, the code inside that case block is executed.
  4. The break statement exits the switch block.
  5. The default case executes if no other cases match.

Example 1: Simple Switch Statement

Let’s see an example where we display the name of a day based on its number:

using System;

class Program
{
    static void Main()
    {
        int day = 3;

        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 number");
                break;
        }
    }
}

Output:

Wednesday

Explanation:

  • The day variable has the value 3.
  • The switch statement checks each case.
  • The case 3 matches, so it prints “Wednesday”.

Example 2: Using the Default Case

Let’s see what happens when the switch expression does not match any case.

using System;

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

        switch (number)
        {
            case 1:
                Console.WriteLine("Number is 1");
                break;
            case 2:
                Console.WriteLine("Number is 2");
                break;
            default:
                Console.WriteLine("Number is not 1 or 2");
                break;
        }
    }
}

Output:

Number is not 1 or 2

Explanation:

  • Since number is 10 and there is no matching case, the default block executes.

Example 3: Switch Statement Without Break

If the break statement is omitted, the program will fall through and execute all subsequent cases.

using System;

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

        switch (number)
        {
            case 1:
                Console.WriteLine("Number is 1");
            case 2:
                Console.WriteLine("Number is 2");
            case 3:
                Console.WriteLine("Number is 3");
                break;
            default:
                Console.WriteLine("Unknown number");
                break;
        }
    }
}

Output:

Number is 2  
Number is 3

Explanation:

  • The number is 2, so it starts executing at case 2.
  • Since there is no break, it also executes case 3.

Example 4: Switch with Multiple Case Labels

You can group multiple cases together if they share the same code:

using System;

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

        switch (number)
        {
            case 1:
            case 2:
            case 3:
                Console.WriteLine("Number is between 1 and 3");
                break;
            case 4:
            case 5:
                Console.WriteLine("Number is either 4 or 5");
                break;
            default:
                Console.WriteLine("Number is out of range");
                break;
        }
    }
}

Output:

Number is either 4 or 5

Explanation:

  • case 4 and case 5 are grouped together and share the same code block.

Switch Statement vs. If…Else

FeatureSwitch StatementIf…Else Statement
ReadabilityCleaner for multiple fixed conditionsBetter for complex conditions
PerformanceFaster when handling many casesSlightly slower in some cases
Use CaseWhen checking a single variableWhen multiple conditions exist

Best Practices for Using Switch

  1. Use Break Statements: Always include a break to avoid fall-through issues.
  2. Include Default Case: The default ensures your program handles unexpected values.
  3. Group Cases: Combine cases with shared logic to reduce redundancy.
  4. Avoid Overusing Switch: Use switch when dealing with fixed, known values.

Conclusion

The switch statement in C# is a powerful tool for handling multiple conditions in a clean and organized way. By understanding its syntax and usage, you can simplify your code and improve its readability.

For more tutorials and hands-on examples, visit The Coding College—your one-stop destination to learn coding and programming.

Leave a Comment