Google+

55. '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.


Note - We can also write switch inside another switch. This can be called as Nested switch statements. I'm not writing an example for this here as this is out of box for selenium automation. If you are interested you can search online for nested switch statements.





Please comment below to feedback or ask questions.

How to use the while iteration statements will be explained in the next post.




3 comments:

MS said...

Hello Arun,

As a good practice which statements should be used in programs from "if" and "switch" ?

Also could you please explain at what situations these statements should be used?

Thanks!

dinesh said...

Hi MS

if your flow of program contains too much if-else statements continuously then there is concern about performance. Then you must go for "switch".


e.g. If you want to check which is current month then in "if" case you have to write 1=> if succeeded by 11=> else statements.

In the above case performance is hampered. And if we go for "switch" there minimum comparison i.e. only 1 resulting in higher performance. so we must go for it.

Arun Motoori said...

@MS - As a programmer, if you are comfortable and are not tried of writing many if-else-if statements, then you can go for if-else-if statements. In case, you are tired of writing many if-else-if statements, we get a feeling of using switch. When you get a feeling go for switch instead.