switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }
Important points to remember:
- In C#, duplicate case values are not allowed.
- The data type of the variable in the switch and value of a case must be of the same type.
- The value of a case must be a constant or a literal. Variables are not allowed.
- The break in switch statement is used to terminate the current sequence.
- The default statement is optional and it can be used anywhere inside the switch statement.
- Multiple default statements are not allowed.
// C# program to illustrate // switch case statement using System;  public class CodeConfig{      // Main Method     public static void Main(String[] args)     {         int nitem = 5;         switch (nitem) {          case 1:             Console.WriteLine( "case 1" );             break ;          case 5:             Console.WriteLine( "case 5" );             break ;          case 9:             Console.WriteLine( "case 9" );             break ;          default :             Console.WriteLine( "No match found" );             break ;         }     } } |
case 5
Why do we use Switch Statements instead of if-else statements?
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. If you use if-else statement for a larger number of possible conditions then, it takes more time to write and also become difficult to read.
Example: Using if-else-if statement
// Below is C# program to illustrate // if-else statement using System;  class CodeConfig {      // Main Method     public static void Main(String[] args)     {         // taking two strings value         string topic;         string category;          // taking topic name         topic = "Inheritance" ;                  // using compare function of string class         if ((String.Compare(topic, "Introduction to C#" ) == 0) ||             (String.Compare(topic, "Variables" ) == 0) ||             (String.Compare(topic, "Data Types" ) == 0))         {             category = "Basic" ;         }                          // using compare function of string class         else if ((String.Compare(topic, "Loops" ) == 0) ||                  (String.Compare(topic, "If Statements" ) == 0) ||                  (String.Compare(topic, "Jump Statements" ) == 0))         {             category = "Control Flow" ;         }                              // using compare function of string class         else if ((String.Compare(topic, "Class & Object" ) == 0) ||                  (String.Compare(topic, "Inheritance" ) == 0) ||                  (String.Compare(topic, "Constructors" ) == 0))         {             category = "OOPS Concept" ;         }                  else         {             category = "Not Mentioned" ;         }          System.Console.Write( "Category is " + category);     } } |
Category is OOPS Concept
Explanation: As shown in the above program the code is not excessive but, it looks complicated to read and took more time to write. So we use a switch statement to save time and write optimized code. Using switch statement will provide a better readability of code.
Example: Using Switch Statement
// C# program to illustrate // switch statement using System;  class CodeConfig {      // Main Method     public static void Main(String[] args)     {         // taking two strings value         string topic;         string category;          // taking topic name         topic = "Inheritance" ;                  // using switch Statement         switch (topic)         {             case "Introduction to C#" :             case "Variables" :             case  "Data Types" :                                   category = "Basic" ;                 break ;                               case "Loops" :              case "If Statements" :              case "Jump Statements" :                                   category = "Control Flow" ;                 break ;                               case "Class & Object" :              case "Inheritance" :              case "Constructors" :                                   category = "OOPS Concepts." ;                 break ;                               // default case              default :                 category = "Not Mentioned" ;                 break ;                          }                  System.Console.Write( "Category is " + category);     } } |
Category is OOPS Concepts.
Using goto in the Switch Statement
You can also use goto statement in place of the break in the switch statement. Generally, we use a break statement to exit from the switch statement. But in some situations, the default statement is required to be executed, so we use the goto statement. It allows executing default condition in the switch statement. The goto statement is also used to jump to a labeled location in C# program.
// C# program to illustrate the // use of goto in switch statement using System; Â public class CodeConfig { Â // Main Method public static void Main(String[] args) Â Â Â Â { Â Â Â Â Â Â Â Â int greeting = 2; Â Â Â Â Â Â Â Â Â switch (greeting) { Â Â Â Â Â Â Â Â case 1: Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine( "Hello" ); Â Â Â Â Â Â Â Â Â Â Â Â goto default ; Â Â Â Â Â Â Â Â case 2: Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine( "Hi" ); Â Â Â Â Â Â Â Â Â Â Â Â goto case 3; Â Â Â Â Â Â Â Â case 3: Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine( "Namaste" ); Â Â Â Â Â Â Â Â Â Â Â Â goto default ; Â Â Â Â Â Â Â Â default : Â Â Â Â Â Â Â Â Â Â Â Â Console.WriteLine( "Entered value is: " + greeting); Â Â Â Â Â Â Â Â Â Â Â Â break ; Â Â Â Â Â Â Â Â } Â Â Â Â } } |
Hi Namaste Entered value is: 2
Explanation: In the above program, the goto statement is used in a switch statement. Here first the case 2, i.e. Hi is printed because the switch contains the value of greeting is 2, then the control transfers to the case 3 due to the presence of goto statement, so it prints Namaste and in last the control transfer to the default condition and print Entered value is: 2.
Note: You can also use continue in place of a break in switch statement if your switch statement is a part of a loop, then continue statement will cause execution to return instantly to the starting of the loop.