Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Demonstrations of switch.Lecture 2 - slide 14 : 43
Program 3
/* Right, Wrong */
using System;

class SwitchDemo {
  public static void Main(){
    int j = 1, k = 1;

    /* 
    switch (j) {                                    
      case 0:  Console.WriteLine("j is 0");         
      case 1:  Console.WriteLine("j is 1");
      case 2:  Console.WriteLine("j is 2");
      default: Console.WriteLine("j is not 0, 1 or 2");
    }   
    */

    switch (k) {                                      
      case 0:  Console.WriteLine("m is 0"); break;    
      case 1:  Console.WriteLine("m is 1"); break;
      case 2:  Console.WriteLine("m is 2"); break;
      default: Console.WriteLine("m is not 0, 1 or 2"); break;
    }   

    switch (k) {                                                 
      case 0: case 1:  Console.WriteLine("n is 0 or 1"); break;  
      case 2: case 3:  Console.WriteLine("n is 2 or 3"); break;
      case 4: case 5:  Console.WriteLine("n is 4 or 5"); break;
      default: Console.WriteLine("n is not 1, 2, 3, 4, or 5"); break;
    }   

    string str = "two"; 
    switch (str) {                                          
      case "zero":  Console.WriteLine("str is 0"); break;   
      case "one":  Console.WriteLine("str is 1"); break;    
      case "two":  Console.WriteLine("str is 2"); break;
      default: Console.WriteLine("str is not 0, 1 or 2"); break;
    }   
  }      
}
 
 
 
 
 
 
 
 
Illegal: Control cannot fall 
through from one case label to another
 
 
 
 
 
 
Legal.
Break or similar jumping is needed
 
 
 
 
 
Legal as well.
Falling through empty cases is possible
 
 
 
 
 
 
Legal. 
It is possible to switch on strings
Would be illegal in C.