Wednesday 5 February 2014

Control flow statements : Decision Making: switch

The body of a switch statement is known as a switch block. A statement in the switch block can be labelled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

switch works with the byte, short, char, and int primitive data types. It supports enums also

java 7 supporting strings also in switch case evaluation, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Integer, Char, Byte, Short are called wrapper classes. Will discuss about these more on coming posts.

Example
class SwitchEx{
 public static void main(String args[]){
  int day = 5;
  
  switch(day){
   case 1:
    System.out.println("Sunday");
    break;
   case 2:
    System.out.println("Monday");
    break;
   case 3:
    System.out.println("Tueday");
    break;
   case 4:
    System.out.println("Wednesday");
    break;
   case 5:
    System.out.println("ThursDay");
    break;
   case 6:
    System.out.println("Friday");
    break;
   case 7:
    System.out.println("Saturday");
    break;
   default:
    System.out.println("You entered wrong day number");
  }
 }
}

Output
ThursDay

Explanation
As you see in the above program, variable “day” set to the value 5. So in the switch case, case 5 is executed. If the day is set to 1, then case 1 will execute and come out of the switch.

You can see in the above program, each case has a break statement associates with it, is it necessary ? Yes of course, if you don't specify the break for case statements, then all the case statement below the evaluated case are executed.

And one more thing is, case statements need not be in proper order. They can be written in any way.

class SwitchEx{
 public static void main(String args[]){
  int day = 5;
  
  switch(day){
   case 2:
    System.out.println("Monday");
   case 3:
    System.out.println("Tueday");
   case 4:
    System.out.println("Wednesday");
   case 5:
    System.out.println("ThursDay");
   case 6:
    System.out.println("Friday");
   case 7:
    System.out.println("Saturday");
   case 1:
    System.out.println("Sunday");
   default:
    System.out.println("You entered wrong day number");
  }
 }
}

Output
ThursDay
Friday
Saturday
Sunday
You entered wrong day number


As you see in the above program, “day” is set to 5, and there is no breaks in the corresponding cases. So case 5, 6,7, 1 and default are executed.

One more thing is in the above program, case 1 came after 7, it is acceptable behaviour in java.

What is the necessity of default here
If no case is evaluated, then default executes, just like else block in if-else if-else ladder.

Example
class SwitchEx{
 public static void main(String args[]){
  int day = 9;
  
  switch(day){
   case 1:
    System.out.println("Sunday");
    break;
   case 2:
    System.out.println("Monday");
    break;
   case 3:
    System.out.println("Tueday");
    break;
   case 4:
    System.out.println("Wednesday");
    break;
   case 5:
    System.out.println("ThursDay");
    break;
   case 6:
    System.out.println("Friday");
    break;
   case 7:
    System.out.println("Saturday");
    break;
   default:
    System.out.println("You entered wrong day number");
  }
 }
}
 
Output
You entered wrong day number

Some points to remember
1. When to use if-else if -else ladder, than switch ?
Switch case won't supports range checks like age>30, year<2000 etc., in those cases better to go for if-else if-else ladder.
Control flow statements                                                 switch statement                                                 Home

No comments:

Post a Comment