Part-7 C# Tutorial Switch-Case

Switch Case

The switch Case is a selection statement. It chooses a single section to execute code of one of the conditions equal to one of the cases based on a pattern match with the specified match expression.the statements following the case are executed. A break statement ends the switch case.The optional Consider the following example


The following rules to apply  on switch statement −


1    You can have any number of case statements within a condition check if valid case  print result is followed by the value and a colon.

2    break statement, you can 'jump out of a case' whereas by using a continue statement,          you can 'jump over one iteration' and then resume your case execution.


3    default case is for when the statement does not equal any of the cases then print a              message.


using System;

public class Example
{
   public static void Main()
   {
      int caseSwitch = 1;
      
      switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }
    Console.ReadLine();
   }
}

If you have if-else statements why should be use Switch case Statement?

We use a switch statement instead of if-else statements because if-else statement only works for a small number of logical evaluations of a value. but in switch case any number 
of case statement.That easy to write switch case.switch is a selection statement that chooses a single switch section to execute from a 
list of candidates based on a pattern match with the match expression.