- Switch Case in Java with Example is most used condition based approach in java.
- It is similar from other programming language like sql, C,C++.
Syntax
switch (key) {
case value:
break;
default:
break;
}
- Switch Case will print matching conditions
- If none of the condition matches default block will execute
- break statement is necessary here
- case statement are case sensitive
Example
public class SwitchCaseExample {
public static void main(String[] args) {
try {
String language = "python";
switch (language) {
case "JAVA":
System.out.println("Java is choosed");
break;
case "C++":
System.out.println("C++ is choosed");
case "python":
System.out.println("Python is choosed");
break;
default:
System.out.println("default");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Python is choosed