Java Switch Case

The Java If Else Statement allows us to choose between TRUE or FALSE and when there are more than two options, we simply use Nested If in Java programming. Say, What if we have 10 alternatives to choose?, if we use Nested If in this situation then programming logic will be difficult to understand. In Java Programming, Else if statement and Switch statement can handle these type of problems effectively. We already discussed about the Java Else If Statement in our previous post so, let us explore Java switch case here.

The working functionality of the switch case in Java programming is almost same as Java If Statement. As we said before, Switch statement may have n number of cases so, switch case compares the expression value with the values assigned in the case statements. If both the values expression value and case value match then statements present in that case statement will execute. Let us see the syntax of switch case for better understanding

Syntax:
switch(expression) { case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; }
Java Switch Case
Example:
public class SwitchExample { public static void main(String[] args) { int number=40; switch(number) { case 10: System.out.println("10");break; case 20: System.out.println("20");break; case 30: System.out.println("30");break; case 40: System.out.println("40");break; case 50: System.out.println("50");break; default:System.out.println("Not in 10, 20,30,40 or 50"); } } }


Output:
40
import java.util.Scanner; public class SwitchExample { public static void main(String[] args) { int number; Scanner p=new Scanner(System.in); System.out.print("Enter any value:"); number=p.nextInt(); switch(number) { case 10: System.out.println("10");break; case 20: System.out.println("20");break; case 30: System.out.println("30");break; case 40: System.out.println("40");break; case 50: System.out.println("50");break; default:System.out.println("The number not equal to 10, 20,30,40 and 50"); } } }


Output:
Enter any value:50 50
Java Switch Statement is fall-through

The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.

Example:
public class SwitchExampleFell { public static void main(String[] args) { int number=30; switch(number){ case 10: System.out.println("10"); case 20: System.out.println("20"); case 30: System.out.println("30"); default:System.out.println("The number is not equal in 10, 20 or 30"); } } }


Output:
30 Not in 10, 20 or 30
import java.util.Scanner; public class SwitchExampleFell { public static void main(String[] args) { int number; Scanner p=new Scanner(System.in); System.out.print("Enter any value:"); number=p.nextInt(); switch(number) { case 10: System.out.println("10"); case 20: System.out.println("20"); case 30: System.out.println("30"); case 40: System.out.println("40"); case 50: System.out.println("50"); default:System.out.println("The number is not equal in 10, 20,30,40 or 50"); } } }


Output:
Enter any value:30 30 40 50 The number is not equal in 10, 20,30,40 or 50



Instagram