Java switch / Chapter -2 / Flow Control



Java switch

java switch statement:-





  • This is the second part of selection Statement. if -else we have already discussed in previous chapter 1 / Flow Control.



  • If multiple options are available then it is not recommended to use nested if-else.To meet this requirement we should go for the switch ( ) Statement.


Syntax:-

                    switch ( x )

                    {


                     case 1: Action -1

                                 break;


                     case 2: Action -2

                                 break;



                     case 3: Action -3

                                 break;




                     case 4: Action -4

                                 break;



                     default : default Action


                      }



  • The allowed arguments type for the switch( ) statement are the byte, short, char, int until 1.4 version.But from 1.5 version onward corresponding wrapper classes and enum type is also allowed. From 1.7 version onward String type also allowed.


switch


  • Curly braces  { } are mandatory in switch ( ) statement.



  •  Both case and default are optional. It means an empty switch statement is a valid java syntax.


example :-
                               int x=20

switch ( x )
                                            {

                                             }


  •  Inside a switch, every statement should be under either case or default i.e. independent statements are not allowed inside a switch otherwise we will get compile time error.


example :-


                           i

nt x=20
switch ( x )
{
System.out.println("hello");
}
Compile Time Error:case , default or { expected


  •  Every case label should compile time constant i.e. constant expression.


int x=10;
int y=20;
switch(x)
{
case 10: System.out.println("hello");
break;

case y  : System.out.println("hii");
break;
}

Compile Time Error: in place of y constant expression required.

Note: If we declare y as final then we won't get compile time error.




  •  Both switch argument and case label can be expression but case label should be a constant expression.(Compile Time Constant)


example:-
int x=20;
switch(x +1)
{
case 11 : System.out.println("hello");
break;

case 10+20+30:  System.out.println("hii");
break;
}

 valid 

  •  Every case label should be in the range of switch arguments type, Otherwise, we will get Compile Time Error.


example (a):-

byte y=20;
switch(y)
{
case 10 :System.out.println("10");
break;

case 100 :System.out.println("100");
break;

case 1000:  System.out.println("1000");
break;

}

Compile Time Error:Possible Loss of Precision
                                        Found: int
                                        Required: byte

example (b):-

int y=20;
switch(y)
{
case 10: System.out.println("10");
break;

case 100: System.out.println("100");
break;

case 1000: System.out.println("1000");
break;

}

Perfectly Valid;

example (c):-

byte y=20;
switch(y+1)
{
case 10: System.out.println("10");
break;

case 100: System.out.println("100");
break;

case 1000: System.out.println("1000");
break;

}

Perfectly Valid; 
                   Hint: Because the argument has become int.

  •  Duplicate case label is not allowed.Otherwise, we will get compile time error.


example:-

int  y=20;
switch(y)
{
case 97 :System.out.println("10");
break;

case 98 :System.out.println("100");
break;

case 'a' :  System.out.println("1000");
break;

}

Compile Time Error:Duplicate case lable 'a'=   97                                     

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com