switch

In this chapter you will learn:

  1. Get to know the syntax of switch statement
  2. Group case statements together
  3. Empty cases can fall through

switch statement syntax

switch statement executes different branches based on the selected value.

It has the following format:

switch(expression) {
   case constant1:
       statement sequence //j a  v a 2s .  co  m
       break;
   case constant2:
       statement sequence 
       break;
   case constant3:
       statement sequence 
       break;
   .
   .
   .
   default:
      statement sequence
      break;
}

The switch expression must be of an integer type, such as char, byte, short, or int, or of type string. The default statement sequence is executed if no case constant matches the expression. The default is optional. If default is not present, no action takes place if all matches fail. When a match is found, the statements associated with that case are executed until the break is encountered.

using System; /*from   ja v  a2 s.  c  o  m*/
 
class MainClass {   
  public static void Main() { 
    int i; 
 
    for(i=0; i<10; i++) 
      switch(i) { 
        case 0:  
          Console.WriteLine("i is zero"); 
          break; 
        case 1:  
          Console.WriteLine("i is one"); 
          break; 
        case 2:  
          Console.WriteLine("i is two"); 
          break; 
        case 3:  
          Console.WriteLine("i is three"); 
          break; 
        case 4:  
          Console.WriteLine("i is four"); 
          break; 
        default:  
          Console.WriteLine("i is five or more"); 
          break; 
      } 
  }   
}

The code above generates the following result.

The following code displays the detailed message for a grading schema.

using System;//  ja va 2  s. c  o m

class Program
{
    static void Main(string[] args)
    {
        string ch = "C";
        switch (ch)
        {
            case "A":
                Console.WriteLine("Excellent");
                break;
            case "B":
                Console.WriteLine("Good");
                break;
            case "C":
                Console.WriteLine("OK");
                break;
            case "D":
                Console.WriteLine("No so good");
                break;
            case "E":
                Console.WriteLine("Failed");
                break;
        }
    }
}

The output:

You can use the string and enum value as the switch control value.

Group case statements together

We can group statements together to shorten the code.

using System;//from  java2s . c om

class Program
{
    static void Main(string[] args)
    {
        string ch = "C";
        switch (ch)
        {
            case "A":
            case "B":
            case "C":
                Console.WriteLine("Pass");
                break;
            case "D":
            case "E":
                Console.WriteLine("Failed");
                break;
        }

    }
}

The output:

Empty cases can fall through

using System; //from  ja  v a  2s .co  m
 
class MainClass {   
  public static void Main() { 
    int i; 
 
    for(i=1; i < 5; i++)  
      switch(i) { 
        case 1: 
        case 2: 
        case 3: Console.WriteLine("i is 1, 2 or 3"); 
          break; 
        case 4: Console.WriteLine("i is 4"); 
          break; 
      } 
 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use break statement
  2. break statement with while loop
  3. Find the smallest factor of a value
  4. Using break with nested loops
  5. Use break with a foreach
Home » C# Tutorial » Statements
Comments
if Statement
while loop
do...while loop
for loop
foreach
switch
break
continue statement
goto statement
Xml Documentation