C# Switch Case

The C# Switch case or statement is useful to choose a single section from a list of switch sections to execute depending upon the match with the match expression.

In general, switch statements are useful in place of if else statements because it is faster than if-else. As we said earlier, the switch case is faster than if-else. Because unlike if else, it prepares a lookup table at compile time to find out the match and directly executes that statement without comparing each condition in the list.

C# Switch Case Syntax

The basic syntax of the switch statement is

switch(expression)
{
 case value 1:
 statements;
 break;
 

 case value 2:
 statements;
 break;
 .
 .
 default:
 statements;
}

Let us see an example code using the C# switch case.

using System;
class Switch_Statement
{
     static void Main()
     {
         Console.WriteLine("Enter your character");
         char vowel = char.Parse(Console.ReadLine());

         switch(vowel)
         {
             case ('a'):
                 Console.WriteLine("you entered a vowel");
                 break;
             case ('e'):
                 Console.WriteLine("you entered a vowel");
                 break;
             case ('i'):
                 Console.WriteLine("you entered a vowel");
                 break;
             case ('o'):
                 Console.WriteLine("you entered a vowel");
                 break;
             case ('u'):
                 Console.WriteLine("you entered a vowel");
                 break;
             default:
                 Console.WriteLine("you entered a consonant");
                 break;
         }
     }
}
C# Switch Case Example 1

Here we have taken a char variable vowel to store the character the user has entered.

Once we entered i as input. The compiler compares character i with the cases, and the case i will result in the match, and hence it prints the output as

You entered a vowel

Next time, we entered the input as k, which gets matched with neither of the cases and hence it prints the default statement.

You entered a consonant.

C# Switch Case Example

In this example, we are creating a single Switch case for Multiple values.

We can create a single case for multiple values when the case statements have no code in between or have a common code for all of them.

A case with no code will fall automatically through the next case. Let us see the way of coding for the same switch case statement example seen above.

using System;
 class Program
 {
   static void Main()
   {
     Console.WriteLine("Enter your character");
     char vowel = char.Parse(Console.ReadLine());
 
     switch(vowel)
     {
       case ('a'):
       case ('e'):
       case ('i'):
       case ('o'):
       case ('u'):
         Console.WriteLine("you entered a vowel");
         break;
       default:
         Console.WriteLine("you entered a consonant");
         break;
     }
   }
 }

OUTPUT

Switch Case Example 2

ANALYSIS

Since a, e, i, o, u are all vowels, and have to execute the same code for all these values. Coming to the Switch case execution, we have given i as input for the char variable vowel. Refer to the C# tutorial.

It checks the case i, and because there is no code for i, It falls through o where no code is available. Finally, it falls through u where it finds the code and prints the output.

You entered a vowel

Enter your character
k
you entered a consonant

Next, we entered k as input where it found no match in any of the cases. Hence it prints the default statement.

You entered a consonant

Categories C#