Switch Statement

The condition where we use this Switch statements is that if there are several options in a conditional block but when we want to choose any particular option from that group of options then we can go for Switch Statement.

By using switch statement we can check multiple if conditions directly. We can execute which condition is appropriate to you and depending upon it, that condition is checked by the JVM based on the given value. And the corresponding satatement will be printed as output .

Syntax:
						
switch(expression) { case constant1: statements; break; case constant2: statements; break; default: statements; break; }

In the above syntax expression type should be char ,byte , int,short ,enum. Case values should be constant values.

Ex:
		
public class Student{ Public static void main (string[ ] args) { String s = “abcd”; switch(s) { case a: system.out.println(a); case b: system.out.println(b); case s: system.out.println(d); } } }

But here in the above example we will get the compile time error because String is not a valid keyword in switch statement. Only the converted type of int values and enum constants are allowed

Byte using in switch:
						
public class Byte { public static void main(String[] args) { byte b = 127; switch (b){ case 1: System.out.println(b); case 127: System.out.println(b); case 128: System.out.println(b); } } }

The above example can get compiletime error Because of byte can accept up to 127.

/*program for switch case using final variable*/

						
public class SwitchCase { public static void main(String[] args) { int b=3; final int a = 2; switch (3) { case a: System.out.println(a); break; case b: System.out.println(b); break; default: System.out.println("default"); break; } } }

In the above example we get compilation problem i.e., case expressions must be constant expressions why because initializing the value “a” as final but “b” is not final in switch case. Case values must be constant values. In future b value might be change so we get compilation problem.

/*program for Switch case without using break*/

						
public class SwitchCase { public static void main(String[] args) { char ch = 'a'; switch (ch) { case 'a': System.out.println("case a"); case 'b': System.out.println("case b"); default: System.out.println("default"); break; } } }

o/p: case a
case b
default

Here in the above example we will get the output of three cases because of the reason we are not included the break statement so the JVM will print all the three cases.

/*program for Switch case */

						
public class SwitchCase { public static void main(String[] args) { char ch = 'a'; switch (ch) { case 'a': System.out.println("case a"); break; case 'b': System.out.println("case b"); break; default : System.out.println("default"); break; case 'c': System.out.println("case c"); break; } } }

In the above example we can get output as a “case a”. The thing which is to be remember here is that when we use the break statement then the required output will only prints by the JVM. Here the loop will be terminated and comes out of the switch statement after displaying the corresponding output which is called by the JVM. .

HTML Comment Box is loading comments...