X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

switch statement :

It allows a variable to be tested for equality against a list of values. Each value is called a case and the variable being switched on is checked for each case.

If several options are possible then it is recommended to use switch statement.

Syntax : switch(expression) { case value : //statement; break; case value : //statement; break; //we can have any number of case statement. default : //statement; }

Example :

class Example { public static void main(String [ ] args) { int a = 1; switch(a) { case 1 : System.out.println("Hello Friends."); break; case 2 : System.out.println("Good Bye Friends."); break; default : System.out.println("See You Again"); } } }

Output :

Here, due to the value of 'a' is 1 , output will be Hello Friends.

If the value of 'a' would be 2 , output would be Good Bye Friends.

Else for any other value of 'a' output would be See You Again


Curly braces are mandatory for switch statement.

Both cases and default are optional inside switch statement.

int a = 1;
switch(a)
{

} // valid

Within a switch, every statements should be under some case or default. Independent statements are not allowed.

int a = 1;
switch(a)
{
 System.out.println("Algo");
} // compile time error : case, default, or '}' expected

Until 1.4 version of Java, the allowed data types for switch argument are byte, short, int, char. But from 1.5 version of Java onwards, in addition to those data types, the corresponding wrapper classes (Byte, Short, Integer, Character) and enum types are allowed. And from 1.7 version of Java onwards, String type is also included.

byte b = 10;
switch(b)
{

} // valid
short s = 10;
switch(s)
{

} // valid
int i = 10;
switch(i)
{

} // valid
char c = 'a';
switch(c)
{

} // valid
Byte b = 10;
switch(b)
{

} // valid
Short s = 10;
switch(s)
{

} // valid
Integer i = 10;
switch(i)
{

} // valid
Character c = 'a';
switch(c)
{

} // valid
String s = "Algo";
switch(s)
{

} // valid
boolean b = true;
switch(b)
{

} /* Compile time error : incompatible types
                          required : int
                          found : boolean */
long l = 10l;
switch(l)
{

} /* Compile time error : possible loss of precision
                          required : int
                          found : long */

Every case label should be within the range of switch argument type otherwise we will get compile time error.

byte b = 10;
switch(b)
{
 case 10 :
 System.out.println("Algo");
 break;
 case 100 :
 System.out.println("Valley");
 break;
 case 1000 : // outside the range of byte
 System.out.println("Java");
 break; 
} /* Compile time error : possible loss of precision
                          required : byte
                          found : int */
byte b = 10;
switch(b + 1) // implicitly converted into int 
{
 case 10 :
 System.out.println("Algo");
 break;
 case 100 :
 System.out.println("Valley");
 break;
 case 1000 :
 System.out.println("Java");
 break; 
} // valid

Every case label should be a valid compile time constant. If we are taking variable as case label then we will get compile time error.

int a = 1;
switch(a)
{
 case a : // variable is not allowed as case label
 System.out.println("Algo");
 break;
 case 2 :
 System.out.println("Valley");
 break;
 case 3 :
 System.out.println("Java");
 break; 
} // Compile time error : constant expression required
final int a = 1;
switch(a)
{
 case a :
 System.out.println("Algo");
 break;
 case 2 :
 System.out.println("Valley");
 break;
 case 3 :
 System.out.println("Java");
 break; 
} // valid

Expressions are allowed for both switch argument and case label but case label should be constant expression.

int a = 1;
switch(a + 1)
{
 case 1 :
 System.out.println("Algo");
 break;
 case 2 :
 System.out.println("Valley");
 break;
 case 2+1 : // constant expression
 System.out.println("Java");
 break; 
} // valid

Duplicate case labels are not allowed in switch.

int a = 65;
switch(a)
{
 case 65 :
 System.out.println("Algo");
 break;
 case 66 :
 System.out.println("Valley");
 break;
 case 'A' :
 System.out.println("Java");
 break; 
} // Compile time error : duplicate case label

fall - through inside switch :

Within a switch statement if any case is matched from case onwards all statements will be executed until break statement or end of switch. This is called fall - through inside switch.

int a = 1;
switch(a)
{
 case 1 :
 System.out.println("Algo");
 case 2 :
 System.out.println("Valley");
 break;
 case 3 :
 System.out.println("Java");
 break; 
} 
/* 
Output : 

Algo
Valley
*/

default case :

We can use default case to define default action. This case will execute if and only if no other case is matched. We can put default case anywhere within the switch but it is convention to put it in last.

int a = 1;
switch(a)
{
 case 1 :
 System.out.println("Valley");
 break;
 case 2 :
 System.out.println("Java");
 break;
 default :
 System.out.println("Algo");
}
/* 
Output : 

Valley
*/
int a = 1;
switch(a)
{
 default :
 System.out.println("Algo");
 case 1 :
 System.out.println("Valley");
 break;
 case 2 :
 System.out.println("Java");
 break;
}
/* 
Output : 

Valley
*/


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.