Wednesday, January 18, 2017

Switch Case in java and its operation

Switch:

---------------------------------------------------------------------------------------------

Rule 1 :
'if' conditional statement is able to allow one condition checking but switch is able to allow multiple condition checking.

Syntax:
--------
Switch(var_name)
{
case 1:
----- instructions---
  break;
case 2:
----- instructions---
  break;
case 3:
----- instructions---
  break;
--------
--------
-------
default:
----- instructions---
  break;
}

Note: In general, we will utilize 'switch' in menu driven application.

EX:1. 
class Test
{
public static void main(String[] args)
{
int i = 10;
switch(i)
{
case 5:
System.out.println("five");
break;
case 10:
System.out.println("ten");
break;
case 15:
System.out.println("fifteen");
break;
case 20:
System.out.println("tweenty");
break;
default:
System.out.println("Number is not in 5,10,15 and 20");
break;
}
}
}
Status: No compilation error.
OP: ten

Rules to Write Switch:

--------------------------------
1. Switch is able to allow the data types like byte, short, int, and char as parameters. switch is unable to allow the data types like long, float and double as parameters.

Note: Up to JAVA6 version, java is not allowing string data type as parameter to switch, but, right from java7 version switch is able to allow string data type as parameter.

EX:1. 
class Test
{
public static void main(String[] args)
{
byte b = 5;
switch(b)
{
case 5:
System.out.println("five");
break;
case 10:
System.out.println("ten");
break;
default:
System.out.println("default");
break;
}
}
}
Status: No compilation Error.
OP: ten

EX:2.
class Test
{
public static void main(String[] args)
{
char c = 'B';
switch(c)
{
case 'A':
System.out.println("A");
break;
case 'B':
System.out.println("B");
break;
case 'C':
System.out.println("C");
break;
default:
System.out.println("default");
break;
}
}
}
Status: No compilation Error.
OP: B

EX:3.
class Test
{
public static void main(String[] args)
{
string str = "AAA";
switch(str)
{
case "AAA":
System.out.println("AAA");
break;
case "BBB":
System.out.println("BBB");
break;
case "CCC":
System.out.println("CCC");
break;
default:
System.out.println("default");
break;
}
}
}
Status: No compilation Error.
OP: AAA
--------------------------------------

Rule 2.
In switch all cases and defaults are optional. we can write switch without cases & with default, or we can write switch with cases &
without default. if we write switch without both cases & defaults still we get no error.

EX:1. 
class Test
{
public static void main(String[] args)
{
int i = 10;
switch(i)
{
case 5:
System.out.println("five");
break;
case 10:
System.out.println("ten");
break;
}
}
}
Status: No compilation Error.

EX:2.
class Test
{
public static void main(String[] args)
{
int i = 10;
switch(i)
{
default:
System.out.println("default");
break;
}
}
}
Status: No compilation error

EX:3.
class Test
{
public static void main(String[] args)
{
int i = 10;
switch(i)
{
}
}
}
Status: No compilation Error.
------------------------------------------

Rule 3:
In switch, break statement is optional, we can write switch without break statement, in this case, JVM will execute all the cases right
from matched case to end of switch.

EX:1.
class Test
{
public static void main(String[] args)
{
int i = 10;
switch(i)
{
case 5:
System.out.println("five");

case 10:
System.out.println("ten");

case 15:
System.out.println("fifteen");

default:
System.out.println("default");

}
}
}
Status: No Compilation Error.
OP:
ten
fifteen
default
-----------------------------------------------------------

Rule 4:
In switch, all cases value must be provided with in the range of the data type, which we provide as parameter.

EX: 1.
class Test
{
public static void main(String[] args)
{
byte b = 126;
switch(b)
{
case 126:
System.out.println("126");
break;
case 127:
System.out.println("127");
break;
case 128:
System.out.println("128");
break;
default:
System.out.println("default");
break;
}
}
}
Status: Compilation Error.
Reason: 128 is not in the range of byte.
---------------------------------------------------------------

Rule 5: 
In switch, all case values must be either direct constants or final constants.

EX:
final int i = 5, j = 10, k = 15, l = 20;
switch(10)
{
case i:
System.out.println("five");
break;
case j:
System.out.println("ten");
break;
case k:
System.out.println("fifteen");
break;
case l:
System.out.println("twenty");
break;
default:
System.out.println("default");
break;
}

Status: No Compilation Error.
OP: ten
-------------------------------------------------------------------------------------------
You may also like: Operation on IF-else

No comments:

Post a Comment