Csharp/CSharp Tutorial/Statement/Switch — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

A string can control a switch statement

using System; 
 
class MainClass {  
  public static void Main() {  
    string[] strs = { "one", "two", "three", "two", "one" }; 
 
    foreach(string s in strs) { 
      switch(s) { 
        case "one": 
          Console.Write(1); 
          break; 
        case "two": 
          Console.Write(2); 
          break; 
        case "three": 
          Console.Write(3); 
          break; 
      } 
    } 
    Console.WriteLine(); 
 
  } 
}
12321

Demonstrate the switch

using System; 
 
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; 
      } 
  }   
}
i is zero
i is one
i is two
i is three
i is four
i is five or more
i is five or more
i is five or more
i is five or more
i is five or more

Empty cases can fall through

using System; 
 
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; 
      } 
 
  } 
}
i is 1, 2 or 3
i is 1, 2 or 3
i is 1, 2 or 3
i is 4

Handle two switch cases

using System;
class MainClass
{
    public static void Main()
    {
        int i = 1;
        
        switch (i)
        {
            case 1:
            case 2:
            // code here handles both 1 and 2
            Console.WriteLine("Low Number");
            break;
            
            case 3:
            Console.WriteLine("3");
            goto case 4;
            
            case 4:
            Console.WriteLine("Middle Number");
            break;
            
            default:
            Console.WriteLine("Default Number");
            break;
        }
    }
}
Low Number

Put Switch statement inside a for loop

using System;
class MainClass
{
   static void Main()
   {
      for (int x = 1; x < 6; x++)
      {
         switch (x)                        
         {
            case 2:                        
               Console.WriteLine("x is {0} -- In Case 2", x);
               break;                      
            case 5:                        
               Console.WriteLine("x is {0} -- In Case 5", x);
               break;                      
            default:                       
               Console.WriteLine("x is {0} -- In Default case", x);
               break;                      
         }
      }
   }
}
x is 1 -- In Default case
x is 2 -- In Case 2
x is 3 -- In Default case
x is 4 -- In Default case
x is 5 -- In Case 5

Switch with only default

using System;
class MainClass
{
   static void Main()
   {
      for (int x = 1; x < 4; x++)
      {
         switch (x)
         {
            default:
               Console.WriteLine("x is {0} -- In Default case", x);
               break;
         }
      }
   }
}
x is 1 -- In Default case
x is 2 -- In Default case
x is 3 -- In Default case

Switch without Default

using System;
class MainClass
{
   static void Main()
   {
      for (int x = 1; x < 6; x++)
      {
         switch (x)
         {
            case 5:
               Console.WriteLine("x is {0} -- In Case 5", x);
               break;
         }
      }
   }
}
x is 5 -- In Case 5

The switch Statement

The general form of the switch statement is


switch(expression) {
   case constant1:
       statement sequence 
       break;
   case constant2:
       statement sequence 
       break;
   case constant3:
       statement sequence 
       break;
   .
   .
   .
   default:
      statement sequence
      break;
}
  1. The switch expression must be of an integer type, such as char, byte, short, or int, or of type string .
  2. The default statement sequence is executed if no case constant matches the expression.
  3. The default is optional.
  4. If default is not present, no action takes place if all matches fail.
  5. When a match is found, the statements associated with that case are executed until the break is encountered.

The switch statement with a user input from keyboard

using System;

public class MainClass {
    public static void Main( ) {
        Console.WriteLine("Please make your selection");
      Console.WriteLine("1 Hamburger");
      Console.WriteLine("2 Cheese Burger");
      Console.WriteLine("3 Fish");
      int Selection = int.Parse( Console.ReadLine( ) );
      switch( Selection ) {
        case 1:
          Console.WriteLine("Hamburger");
          break;
    
        case 2:
          Console.WriteLine("Cheese Burger");
          break;
    
        case 3:
          Console.WriteLine("Fish");
          break;
    
        default:
          Console.WriteLine("Unknown choice");
          break;
      }
    }
}
Please make your selection
1 Hamburger
2 Cheese Burger
3 Fish
1
Hamburger

Use a char to control the switch.

using System; 
 
class MainClass {   
  public static void Main() { 
    char ch; 
 
    for(ch="A"; ch<= "E"; ch++) 
      switch(ch) { 
        case "A":  
          Console.WriteLine("ch is A"); 
          break; 
        case "B":  
          Console.WriteLine("ch is B"); 
          break; 
        case "C":  
          Console.WriteLine("ch is C"); 
          break; 
        case "D":  
          Console.WriteLine("ch is D"); 
          break; 
        case "E":  
          Console.WriteLine("ch is E"); 
          break; 
      }     
  } 
}
ch is A
ch is B
ch is C
ch is D
ch is E