C# Switch

Switch statement evaluates the value of an expression and try to find a match in a list of case constants. If found, the control will switch to the statements following the case. Otherwise, the control will go to the default code if default block is defined.

Syntax:

switch(expression){
    case constant1:
       code 1;
       <break;>
    case constant2:
       code 2;
       <break;>
    ...
    
    <default:>
       code n;
       <break;>
}

C# switch statement diagram

  • Firstly we'll evaluate the value of the expression in the switch clause.
  • Secondly, we'll check each constant in the case clause trying to find a match.
  • If we find expression == constant x, then the code following the case clause will be executed.
  • If we can not find the match, we'll execute default code if it exists.
  • When break statement is met, the control will jump outside the switch statement and continue running the following code.
  • Default and break are all optional statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;

namespace TestSwitchStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            int test = 2;

            switch( test ){
                case 1:
                    Console.WriteLine("You chose 1.");
                    break;
                case 2:
                    Console.WriteLine("You chose 2.");
                    break;
                    Console.WriteLine("The statement will never be run.");
                case 3:
                case 4:
                    Console.WriteLine("You chose 3 or 4.");
                    break;
                default:
                    Console.WriteLine("The test number cannot be found in above cases.");
                    break;
            }
            Console.Read();
        }
    }
}

Output

You chose 2.
  • Line 11: Start Switch statement to evaluate the value of test.
  • Line 12: If test == 1, we'll run the following code (Line 13-14).
  • Line 13-14: Output "You chose 1." and then jump out switch statement and start to run line 27.
  • Line 15-17: If test == 2, we'll run the Line 16-17 code.
  • Line 18: Because line 17 is a break statement, this line can not be reachable. You will get a warning "Unreachable code detected" when the example is compiled.
  • Line 19-20: You can combine some cases together to run the same code. These lines mean if test equals 3 or 4, line 21-22 will be run.
  • Line 23: Default clause. It is similar to the Else clause in the If statement. the following code (line 24-25) will be run if no match can be found the above cases.
  • Line 25: This break statement could not be omitted even if it was the last statement.
  • Change the location of the default clause and see what will be happened?
            switch( test ){
                case 1:
                    Console.WriteLine("You chose 1.");
                    break;
                case 2:
                    Console.WriteLine("You chose 2.");
                    break;
                    Console.WriteLine("The statement will never be run.");
                default:
                    Console.WriteLine("The test number cannot be found in above cases.");
                    break;
                case 3:
                case 4:
                    Console.WriteLine("You chose 3 or 4.");
                    break;
            }

You will get the same result of example 01-27-01.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;

namespace TestSwitchExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char guess = 'A';

            switch( guess ){
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                    Console.WriteLine("It is A, a ,B or b.");
                    break;
                case 'C':
                case 'c':
                    Console.WriteLine("It is C or c.");
                    break;					
            }
            Console.Read();
        }
    }
}

Output

It is A, a ,B or b.
  • Line 11: The switch statement starts.
  • Line 12-15: 4 cases are bundled together. If the variable guess matches either of them, the following code line16-17 will be executed.
  • Line 16: Output the string.
  • Line 18-21: case 'C' or 'c' and its code block.
  • There is no default block so if not matched, nothing will be happened. The code after switch statement continue running.