Search

Sunday, April 11, 2021

Java Switch



Switch statement in Java


Sometimes we want to test a value of a variable with different cases. For this purpose, we have switch statement in Java language. we can execute    the block of the statements in case when we have a match with the switch   case. the value inside the variable is checked with all the cases inside the    switch statement and when the value is matched with any of the switch case we can execute block of statements.
In case if there is no match, then we have a separate area known as default the statements 
inside default block will be executed.


The syntax of the switch statement is as follow

switch( expression )

{

Case Label:

Statement;

Break;

Case Label:

Statement;

Break;

default:

Statement;

break;

}



1. Here we have expression inside the switch parenthesis 

2. After that we have cases with their own labels

3. The label of the cases should end with colon

The break keyword is very important in case of switch statement because     this one is used to stop the execution of particular case. if we do not use the break keyword then Java will execute every case after a value is matched   with particular case. so break keyword helps us to stop the execution of the case and control will 
transfer outside of switch block

Finally we have a default case which is used in case when we have no match with any of the switch cases

For Example

class SwitchTest
{

        public static void main(String[] ar)

{

                int number=5;

                switch(number)

                {

                        case 1:

                        System.out.println(number);

                        break;

                        case 2:

                        System.out.println(number);

                        break;

                        case 3:

                        System.out.println(number);

                        break;

                        case 4:

                        System.out.println(number);

                        break;

                        case 5:

                        System.out.println(number);

                        break;

                        default:

                        System.out.println(“Nothing”);

                        break;

                }

        }

}

Output:

5


In the above example, we have a variable number having value 5. Then in switch, that variable value is checked with its cases. Now as the value inside number variable is  5, switch will compare it with every case, and as this value is matched with case 5, so the block of case 5 will be executed and we will get the output as 5

If we change the value inside variable, then we will get another result.


For Example

class SwitchTest
{

        public static void main(String[] ar)

        {

                int number=2;

                switch(number)

                {

                        case 1:

                        System.out.println(number);

                        break;

                        case 2:

                        System.out.println(number);

                        break;

                        case 3:

                        System.out.println(number);

                        break;

                        case 4:

                        System.out.println(number);

                        break;

case 5:

                        System.out.println(number);

                        break;

                        default:

                        System.out.println(“Nothing”);

                        break;

                }

        }

}

Output:

2

Now we have answer 2 because we have initialized the variable number with value 2, so switch will compare it with first case and as the value of first case is 1 so the block of first case will not be executed.

It will check the value of variable with case 2 and now as the value is matched with case 2, so the block of case 2 will be executed and because of break statement, cursor will come out from the switch body

Also if none of the case is matched with the variable value, then default block will be executed at the end.

 

How Switch statement works


First of all the expression is compared with the cases of the switch, and if     the expression is matched with the first case, then the code of first case will be executed

If the expression is matched with the second case then the code inside the   second case will be executed and so on

If expression is not matched with any of the switch cases, the code of the    default case is executed

The switch statement in Java language works like Java if else if but switch is much cleaner and easy 
to understand



Use of break in Java switch


Break statement in Java cases is used to stop the execution of the block of code  and if we don't use break statement then every case block will be executed after the expression is matched with some case

Example

class SwitchTest
{

        public static void main(String[] ar)

{

                int number=1;

                switch(number)

                {

                        case 1:

                        System.out.println(“ist”);

                        case 2:

                        System.out.println(“second”);

                        case 3:

                        System.out.println(“third”);

                        case 4:

                        System.out.println(“Fourth”);

                        case 5:

                        System.out.println(“Fifth”);

                       

                        default:

                        System.out.println(“Default”);

               

                }

        }

}


Output:

Ist Second Third Fourth Fifth Default

 

In above example, expression is matched with first case and as there is no  break statement, so all the cases after first case will also execute. so we     must put the break statement in order stop the execution of other cases

The default case of Switch Statement in Java


The default case is used when there is no match with the switch cases. so it  is executed at the end

Here is the example to describe it

 

class SwitchTest
{

        public static void main(String[] ar)

{

                int number=50;

                switch(number)

                {

                        case 1:

                        System.out.println(number);

                        break;

                        case 2:

                        System.out.println(number);

                        break;

                        case 3:

                        System.out.println(number);

                        break;

                        case 4:

                        System.out.println(number);

                        break;

                         case 5:

                        System.out.println(number);

                        break;

                        default:

                        System.out.println(“Nothing”);

                        break;

                }

        }

}

Output:

Nothing

 

Because, none of the case is matched with the variable value here, so default case block is executed and we are getting output as Nothing

 

Nested Switch Statement in Java


When we have on switch statement inside another switch statement, this is known as Nested Switch statement. The syntax for nested switch is very simple


Syntax

Switch(expression)

{

        Switch(expression)
        {

        }

}

 

Following is the program for Nested Switch


class NestedSwitch
{

        public static void main(String[] ar)

        {

                int number1=1, number2=2;

                switch(number1)

                {

                        case 1:

                                switch(number2)

                                {

                                        case 2:

                                        System.out.println(“Welcome to Java Language”);

                                        break;

                                        case 3:

                                        System.out.println(“Some message here”);

                                        break;

                                        case 4:

                                        System.out.println(“Some message here”);

                                        break;

                                        default:

                                        System.out.println(“Some message here”);

                                        break;

 

                                }

                        case 2:

                        System.out.println(“some message here”);

                        break;

                        default:

                        System.out.println(“Some message here”);

                        break;

                               

                }

        }

}

 

Output:

Welcome to Java Language

 

As we have value 1 in number1 variable, so case 1 of first switch will be executed and we have another switch inside case 1 body, and it will check number2 variable value and continue its execution


Summary


We use switch statement in Java because it provides cleaner look as compared to if else statement. If we have lots of conditions to check, then if else structure can be complex and program readability is also become complex and difficult to understand

No comments:

Post a Comment