'switch' selection statements

switch provides a better alternative for a large series of if-else-if ladder. 'switch' is another 'Control Flow' statement.


Program to demonstrate the 'switch' selection                                                                                

class SwitchDemo
{
   public static void main(String args[])
   {
          int a=2;

         switch(a)
        {
            case 0:
                System.out.println("The value of a is zero");
                break;

            case 1:
                System.out.println("The value of a is one");
                break;

            case 2:
                System.out.println("The value of a is two");
                break;

            default:
                System.out.println("The value of a is "+a);
         }
   }
}

Output of this program:

The value of a is two

switch(a) i.e. switch(2) in this example : As we have mentioned switch(2), the program will directly go to case 2: and execute all the statements under case 2 i..e first it will print 'The value of a is two' and will execute the break; statement ( break; will make the program execution come out of the switch case without executing the next cases)
Program to demonstrate what happens when we don't write break; statement for all the cases

class SwitchWithoutBreak
{
   public static void main(String args[])
   {
      int a=0;

      switch(a)
      {

        case 0:
           System.out.println("The value of a is zero");

        case 1:
           System.out.println("The value of a is one");

        case 2:
           System.out.println("The value of a is two");

         default:
           System.out.println("The value of a is "+a);

      }
   }
}

Output of this program:

The value of a is zero
The value of a is one
The value of a is two
The value of a is 0

switch(a) i.e. switch(0) in this example: As we've mentioned switch(0), the program execution will go to case 0: and execute all the statements under case 0, i.e. it will print "The value of a is zero". Since we've not written break; statement under case 0, the program execution will go to next case i.e. case 1 and will print :The value of a is one". Since the break; statement is not written under case 1, the program execution will go to case 2 and prints "The value of a is two". Since the break; statement is not provided in the case 2, the program execution will go to case default and prints "The value of a is 0".
Program to demonstrate what happens when the expression provided don't match any case

class SwitchExpressionDontMatch
{
   public static void main(String args[])
   {
      int a=5;

      switch(a)    // switch(a) i.e switch(5), there is no case 5 below
      {

        case 0:
           System.out.println("The value of a is zero");

        case 1:
           System.out.println("The value of a is one");

        case 2:
           System.out.println("The value of a is two");

         default:
           System.out.println("The value of a is "+a);

      }
   }
}

Output of this program:

The value of a is 5

switch(a) i.e. switch(5): After executing switch(5) in this program, the program searches for case 5 for directly jumping to that case and executing the steps under case 5. But as we don't have case 5 written in this example, the program will jump to case default and execute the statements under it i.e. in this example the program prints "The value of a is 5"
Program to demonstrate the switch with a different expression

class SwitchExpression
{
   public static void main(String args[])
   {
      int a=1,b=1;

      switch(a+b)   //Used expression 'a+b' instead of simple variable 'a' here
      {

        case 0:
           System.out.println("The value of a+b is zero");
           break;
        case 1:
           System.out.println("The value of a+b is one");
           break;
        case 2:
           System.out.println("The value of a+b is two");
           break;
        default:
           System.out.println("The value of a+b is "+(a+b));

      }
   }
}

Output of this program:

The value of a+b is two

switch(a+b) -> switch(1+1) -> switch(2): Switch(2) will jump directly to case 2 and execute the statements under case 2. Hence this program printed "The value of a+b is two" and executed break; to get out of the switch.