Branch statement: C # how to use switch statement? Examples of usage

Time:2021-4-14

In c#,Switch statementyesMultiple branch statement. It provides an efficient way to transfer execution to different parts of the code based on the value of the expression. A switch expression is an integer type, such as int, char, byte or short, or an enumeration type, or a string type. Check the different cases of the expression, and then perform a match.

The syntax is as follows:

switch (expression) {

case value1: // statement sequence
     break;

case value2: // statement sequence
     break;
.
.
.
case valueN: // statement sequence
     break;

default:    // default statement sequence
}

Switch execution flow chart:
Branch statement: C # how to use switch statement? Examples of usage

Key points to remember:

  • Duplicate case values are not allowed in C #.
  • The data type of the variable in the switch and the value of the case must be of the same type.
  • The value of the case must be a constant or literal. Variables are not allowed.
  • The break in switch statement is used to terminate the current sequence.
  • The default statement is optional and can be used anywhere in the switch statement.
  • Multiple default statements are not allowed.

example:

// C# program to illustrate
// switch case statement
using System;
  
public class GFG {
  
     // 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 ;
         }
     }
}

The output is as follows:

case 5

Why do we use switch statements instead of if else statements?

We use the switch statement instead of the if else statement, because the if else statement only applies to a small number of logical evaluations of values. If you use if else statements for more possible conditions, it will take more time to write and become difficult to read.

Example: using an if else if statement

// C# program to illustrate
// if-else statement
using System;
  
class GFG {
  
     // 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);
     }
}

The output is as follows:

Category is OOPS Concept

Note: as shown in the above program, the code is not excessive, but it looks very complex and takes more time to write. Therefore, we use the switch statement to save time and write optimized code. Using the switch statement will provide better code readability.

Example: using the switch statement

// C# program to illustrate
// switch statement
using System;
  
class GFG {
  
     // 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 Concept" ;
                 break ;
                  
              // default case 
              default :
                 category = "Not Mentioned" ;
                 break ;
                  
         }
          
         System.Console.Write( "Category is " + category);
     }
}

The output is as follows:

Category is OOPS Concept

Using goto in switch statements

You can also use the to statement instead of the break in the switch statement. Usually, we use the break statement to exit from the switch statement. But in some cases, the default statement needs to be executed, so we use the goto statement. It allows default conditions to be executed in a switch statement. Goto statements are also used to jump to tag positions in C # programs.

example:

// C# program to illustrate the
// use of goto in switch statement
using System;
  
public class GFG {
  
// Main Method
public static void Main(String[] args)
     {
         int greeting = 2;
  
         switch (greeting) {
         case 1:
             Console.WriteLine( "Hello" );
             goto default ;
         case 2:
             Console.WriteLine( "Bonjour" );
             goto case 3;
         case 3:
             Console.WriteLine( "Namaste" );
             goto default ;
         default :
             Console.WriteLine( "Entered value is: " + greeting);
             break ;
         }
     }
}

The output is as follows:

Bonjour
Namaste
Entered value is: 2

Note: in the above program, goto statement is used in switch statement. The first case is case 2, that is, the switch is printed because the value of greeting is 2. Then, due to the goto statement, the control is transferred to case 3, so it prints Namaste. Finally, the control is transferred to the default condition and the entered value is printed as 2.

Note: if your switch statement is part of a loop, you can also use continue instead of the break statement in the break statement. The continue statement will immediately return execution to the beginning of the loop.

For more information about C # development, please refer to:Lsbin – it development technologyhttps://www.lsbin.com/

See more about C # below:

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]