Wednesday, August 28, 2013

Switch statement


This is used to test equality of values of set of values. We used 'case' as a name of each values. This is the syntax of switch statement.


Switch (expression)
{
     case value1:
       block 1;
       break;
    
     case value1:
        block 1;
        break;

     case value1:
        block 1;
        break;

     default:
        default block;
        break;
}


  • If you are working with java 7, you can use byte, short, int, enum and also strings for switch case. We are going to learn about enum in next lessons.
  • Look at the following example

public class SwitchDemo {

 public static void main(String args[]) {

  int key = 3;

  switch (key) {
  case 1:
   System.out.println("Key is 0");
   break;

  case 2:
   System.out.println("Key is 1");
   break;

  case 3:
   System.out.println("Key is 2");
   break;

  case 4:
   System.out.println("Key is 3");
   break;

  default:
   System.out.println("Key is not there");
   break;
  }
 }
}
  • You can also try without using "break" keyword. This keyword is used to break the running process when it found the correct value.
  • Default is another keyword which is used to give a default message.
  • If there is no matching case according to your key, this default case will be executed.


Switch with enum


enum Day {
 Sunday, Monday, Friday
}

public class SwitchEnumDemo {

 public static void main(String args[]) {
  Day d = Day.Monday;
  switch (d) {
  case Sunday:
   System.out.println("Today is Sunday");
   break;
  case Monday:
   System.out.println("Today is Monday");
   break;
  case Friday:
   System.out.println("Today is Friday");
   break;
  default:
   System.out.println("Invalid input");
  }
 }
}

Illegal switch statements



public class SwitchDemo {

 public static void main(String args[]) {

  final int x = 1;
  int y;
  y = 2;
  int z = 0;

  switch (z) {

  case x: // OK
  case y: // compile time error
  //Case values must be final variables.
  }
 }
}




public class SwitchDemo {

 public static void main(String args[]) {

  byte x = 2;

  switch (x) {
  case 23:
  case 128: // compile time error
  // Range of byte is -128 to 127
   
  }
 }
}



public class SwitchDemo{
    
    public static void main(String args[]){

         int num = 20;
        
         switch(num) {
         case 10:
             System.out.println("10");
         case 10:
             System.out.println("10"); //compile time error
             //Same value cases are not allowed.
         case 15:
             System.out.println("15");
         default:
             System.out.println("Other");
         }
    }

}


Simple calculator example using switch

  • You need to have Java 1.7 to use Strings as the case.


public class SimpleCalculator {

 public static void main(String args[]) {

  Scanner scn = new Scanner(System.in);

  System.out.print("Enter number one: ");
  double num1 = scn.nextDouble();

  System.out.print("Enter number two: ");
  double num2 = scn.nextDouble();

  System.out.println("Enter your operator: ");
  String op = scn.next();

  switch (op) {

  case "+":
   System.out.println("Sum: " + (num1 + num2));
   break;

  case "-":
   System.out.println("Substraction: " + (num1 - num2));
   break;

  case "*":
   System.out.println("Multiplication: " + (num1 * num2));
   break;

  case "/":
   System.out.println("Devision : " + (num1 / num2));
   break;

  default:
   System.out.println("Invalid input");
   break;
  }
 }
}