Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering the C# Switch Statement: Syntax, Examples & Best Practices

C# switch Statement

Discover how to replace complex if‑else chains with a clean, readable switch statement in C#—complete with practical examples.

Switch statements simplify decision‑making by evaluating a single expression and executing the matching case block. Compared to a long if‑else ladder, the switch syntax is more concise and easier to maintain.

The basic structure is:

switch (variableOrExpression)
{
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    // …
    default:
        // Code when no case matches
}

When the expression matches a case, the corresponding block runs until a break terminates the switch. If no case matches, the default block executes, mirroring an else clause.

Without break, execution would “fall through” and continue into subsequent cases—often leading to bugs. For a deeper dive into C# break statement, refer to the official documentation.


Example 1: Identifying Vowels

using System;

namespace Conditional
{
    class SwitchCase
    {
        public static void Main(string[] args)
        {
            char ch;
            Console.WriteLine("Enter an alphabet");
            ch = Convert.ToChar(Console.ReadLine());

            switch(Char.ToLower(ch))
            {
                case 'a':
                    Console.WriteLine("Vowel");
                    break;
                case 'e':
                    Console.WriteLine("Vowel");
                    break;
                case 'i':
                    Console.WriteLine("Vowel");
                    break;
                case 'o':
                    Console.WriteLine("Vowel");
                    break;
                case 'u':
                    Console.WriteLine("Vowel");
                    break;
                default:
                    Console.WriteLine("Not a vowel");
                    break;
            }
        }
    }
}

Running the program produces:

Enter an alphabet
X
Not a vowel

Here, the user’s input is converted to lowercase and matched against vowel cases. If no vowel matches, the default message is displayed.

Because all vowel cases perform the same action, they can be grouped:


Example 2: Grouped Cases

using System;

namespace Conditional
{
    class SwitchCase
    {
        public static void Main(string[] args)
        {
            char ch;
            Console.WriteLine("Enter an alphabet");
            ch = Convert.ToChar(Console.ReadLine());

            switch(Char.ToLower(ch))
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    Console.WriteLine("Vowel");
                    break;
                default:
                    Console.WriteLine("Not a vowel");
                    break;
            }
        }
    }
}

The output remains identical, but the code is noticeably cleaner.

Switch statements are limited to specific data types:


Example 3: Simple Calculator

using System;

namespace Conditional
{
    class SwitchCase
    {
        public static void Main(string[] args)
        {
            char op;
            double first, second, result;

            Console.Write("Enter first number: ");
            first = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter second number: ");
            second = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter operator (+, -, *, /): ");
            op = (char)Console.Read();

            switch(op)
            {
                case '+':
                    result = first + second;
                    Console.WriteLine("{0} + {1} = {2}", first, second, result);
                    break;
                case '-':
                    result = first - second;
                    Console.WriteLine("{0} - {1} = {2}", first, second, result);
                    break;
                case '*':
                    result = first * second;
                    Console.WriteLine("{0} * {1} = {2}", first, second, result);
                    break;
                case '/':
                    result = first / second;
                    Console.WriteLine("{0} / {1} = {2}", first, second, result);
                    break;
                default:
                    Console.WriteLine("Invalid Operator");
                    break;
            }
        }
    }
}

Example run:

Enter first number: -13.11
Enter second number: 2.41
Enter operator (+, -, *, /): *
-13.11 * 2.41 = -31.5951

This calculator accepts two operands and an operator, then performs the requested operation using a switch. Inputs are read via ReadLine() and Read(); for more on input/output, see C# Basic Input and Output.

While an if‑else chain could replace the switch, the latter offers clearer intent and faster execution for discrete values.


C Language

  1. C# Break Statement: How & When to Use It
  2. Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
  3. Mastering the C++ break Statement
  4. Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
  5. Mastering C Conditional Statements: If, Else, and More
  6. Mastering the C Switch Statement: Syntax, Flow, and Practical Example
  7. Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
  8. Mastering VHDL Generate Statements: Build Reusable Debouncers with Ease
  9. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  10. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices