Switch Case Statement in C# Programming

switch case is a conditional statement where a switch statement compares a variable with a number of provided values called cases and executes a block of statements for each case.

Syntax for switch case Statement

switch (expression)
{
    case value1:
    {
        statements;
        break;
    case value2:
        statements;
        break;
    .
    .
    .
    .
    default:
        statements;
}

Flowchart for switch case Statement

Key points for switch case statement

  • Each case statement is followed by a colon and statements for that case follows after that.
  • The expression in switch statement must have a certain data type that is supported by switch statement like int, char, string, enum etc. The data type cannot be an array or float.
  • Any number of cases can be present in a switch. Each case contain a value and statements to be executed. The value in case must be of same data type as expression in switch statement.
  • Each case statement ends with a break statement. This exits the switch statement. If break statement is not used, following case is also checked even if required case has already been found.
  • Many case statements can execute a same statement by using multiple cases together.
    Example:
      case 1:
      case 2:
      case 3:
      Console.WriteLine("First three cases.");
  • Switch statement also consists of a default statement which is usually at the end of the switch. It is usually used to handle exceptional cases.
  • Arranging cases according to their value name or number order is considered a good practice. Same goes for their occurrence. It is better when most common occurring case is placed at the first.

Example 1: C# example for switch case Statement


C# Program to perform an operation of user choice for addition, subtraction, multiplication and division

using System;
namespace conditional
{
    class Program
    {
        static void Main()
        {
            int a, b, choice;
            Console.WriteLine("Enter first number:");
            a = (int)Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number:");
            b = (int)Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the number operation you want to perform from the menu.");
            Console.WriteLine("1) Addition");
            Console.WriteLine("2) Subtraction");
            Console.WriteLine("3) Multiply");
            Console.WriteLine("4) Divide");
            Console.Write("Choice: ");
            choice = (int)Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    Console.WriteLine(a + b);
                    break;
                case 2:
                    Console.WriteLine(a - b);
                    break;
                case 3:
                    Console.WriteLine(a * b);
                    break;
                case 4:
                    Console.WriteLine(a / b);
                    break;
                default:
                    Console.WriteLine("Invalid choice!");
                    break;
            }
            Console.ReadLine();
        }
    }
}

In this program, user is asked to enter two numbers. Then, a menu is displayed where users have to choose an operation from a numbered list. The entered number is passed onto the switch statement and according to user choice, an operation is performed.

Output:

Enter first number:
2
Enter second number:
3
Enter the number operation you want to perform from the menu.
1) Addition
​2) Subtraction
3) Multiply
4) Divide
Choice: 1
5