A switch statement is used to check a variable value against list of other values. The values are called cases, and can be of any datatype.

Switch Statement are useful in programming when you want to make a decision based on a variable value. It’s easier than using ‘If’ statement for larger set of values to check the variable against.

Syntax of switch statement

            switch (expression)
            {
                case 1:
                    statement 1;
                    break;
                case 2:
                    statement 2;
                    break;
                    .
                    .
                    .
                case n:
                    statement n;
                    break;
                default:
                    statement d;
            }

Example of switch statement

        public static void Main()
        {
            string str = "1";
            switch (str)
            {
                case "1":
                    Console.WriteLine("Case 1 statement");
                    break;
                case "2":
                    Console.WriteLine("Case 2 statement");
                    break;
                default:
                    Console.WriteLine("Default case statement");
                    break;
            }
        }

Output:

Case 1 statement

Switch statement start by an expression to be evaluated, which is followed by number of cases to be evaluated against.  Note that expression variable and the list of cases must be from the same datatype.

Each case contains a code to be performed if the case matched the expression. The switch statement will break when a expression is matched with a case, so the ‘break’ keywords is used.

A switch statement cannot have a case listed for more than once.

The statement has a default case if no other cases matched the expression. The code in the default is executed only if none of the cases matched the switch expression.

Flowchart of switch case

Example I

   static void Main(string[] args)
        {
            int day = 3;
            switch (day)
            {
                case 1:
                    Console.WriteLine("Sunday");
                    break;
                case 2:
                    Console.WriteLine("Monday");
                    break;
                case 3:
                    Console.WriteLine("Tuesday");
                    break;
                case 4:
                    Console.WriteLine("Wednesday");
                    break;
                case 5:
                    Console.WriteLine("Thursday");
                    break;
                case 6:
                    Console.WriteLine("Friday");
                    break;
                case 7:
                    Console.WriteLine("Saturday");
                    break;
                default:
                    Console.WriteLine("Invalid day!");
                    break;
            }
            Console.ReadLine();
        }

When the above code is compiled and executed, it produces the following result.

Output

Tuesday

Example II: Default value switch case example

 static void Main(string[] args)
        {
            int i = 3; ;
            switch (i)
            {
                case 1:
                    Console.WriteLine("One");
                    break;
                case 2:
                    Console.WriteLine("Two");
                    break;
                default:
                    Console.WriteLine("Other"); /* Default Statement */
                    break;
            }
            Console.ReadKey();
        }

The code above is an example of the default section in a switch statement. The value of ‘i’ is 3, which doesn’t match any of the list value. So, the code in the default section will be executed.

Output:

Other

Example III: Multiple case values switch statement

   static void Main(string[] args)
        {
            string state = "New York";
            switch (state)
            {
                case "Michigan":
                case "California":
                case "New York":
                    Console.WriteLine("US State");
                    break;
                case "Alberta":
                case "Quebec":
                    Console.WriteLine("Canadian State");
                    break;
                default:
                    Console.WriteLine("Other");
                    break;
            }
            Console.ReadKey();
        }

The example above checks the expression for multiple values and executes the code when in the section where one of the values is equal to the switch expression.

Output:

US State


The case expression is always checked to be equal the case values. The code below is incorrect:

Case a > 1:
Case b <> 0:

Instead, use the ‘if’ statement to check expression other than equal, such as greater than or less than.

            if (a > 1)
            {
                // Execute Code
            }
            if (b <> 0)
            {
                // Execute Code
            }

 

Last modified: July 28, 2018

Comments

Write a Reply or Comment

Your email address will not be published.