top of page
Writer's pictureThe Tech Platform

Switch Statement in C#

In C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type. The expression is checked for different cases and the one match is executed.

Points to Remember:

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, then it will raise a compile time error.

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true.


Break Keyword:

When C# reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing.


Default Keyword:

The default keyword is optional and specifies some code to run if there is no case match:


Syntax:

switch (expression) {
     case value 1: // statement sequence
     break;

     case value 2: // statement sequence
     break;
     .
     .
     .
     case value N: // statement sequence
     break;

     default:    // default statement sequence
}

Example:

using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      int day = 7;
      Console.WriteLine("This is the Example of Switch Statement in C#");
  
      switch (day) 
      {
        case 1:
          Console.WriteLine("Monday");
          break;
        case 2:
          Console.WriteLine("Tuesday");
          break;
        case 3:
          Console.WriteLine("Wednesday");
          break;
        case 4:
          Console.WriteLine("Thursday");
          break;
        case 5:
          Console.WriteLine("Friday");
          break;
        case 6:
          Console.WriteLine("Saturday");
          break;
        case 7:
          Console.WriteLine("Sunday");
          break;
      }    

    }
  }
}

Output:








Features of switch statement

  • To execute multiple statements, in any case, there is no requirement of braces as in if else.

  • The default may or may not use the break statement.

  • We can use case number in ascending or descending order or in any random order.

  • In both switch and case, we use int or char constant.

  • We can use default anywhere in the program because every time default is executed only when there is no match of any case.

  • We can execute a common set of statement for multiple cases.

  • If there is any statement in switch then it must be in any case.


Disadvantages of switch statement

  1. float constant cannot be used in the switch as well as in the case.

  2. You can not use the variable expression in case.

  3. You cannot use the same constant in two different cases.

  4. We cannot use the relational expression in case.



The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page