Java switch statement 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.
The switch statement can be used when multiple if-else statements are required. It can have multiple code blocks along with the case values and execute one of many code blocks based on the matched case value.
SyntaxThe syntax of Java switch statement is −
switch(expression) { case value : // Statements break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements }Rules for Using switch Statement
The following rules apply to a switch statement −
In this example, we're showing use of switch statement where cases are based on a char. We've created a variable grade. Based on value of grade, each case is checked. if a case is satisfied and break statement is present then following cases are not checked.
public class Test { public static void main(String args[]) { char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }Output
Compile and run the above program using various command line arguments. This will produce the following result −
Well done Your grade is CExample 2
In this example, we're showing use of switch statement where cases are based on a int. We've created a variable grade. Based on value of grade, each case is checked. if a case is satisfied and break statement is present then following cases are not checked.
public class Test { public static void main(String args[]) { int grade = 3; switch(grade) { case 1 : System.out.println("Excellent!"); break; case 2 : case 3 : System.out.println("Well done"); break; case 4 : System.out.println("You passed"); case 5 : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }Output
Compile and run the above program using various command line arguments. This will produce the following result −
Well done Your grade is 3Example 3
In this example, we're showing use of switch statement where cases are based on a String. We've created a variable grade. Based on value of grade, each case is checked. if a case is satisfied and break statement is present then following cases are not checked.
public class Test { public static void main(String args[]) { String grade = "C"; switch(grade) { case "A" : System.out.println("Excellent!"); break; case "B" : case "C" : System.out.println("Well done"); break; case "D" : System.out.println("You passed"); case "F" : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }Output
Compile and run the above program using various command line arguments. This will produce the following result −
Well done Your grade is CUsing default Keyword With switch Statement
The default keyword is used to specify a code block when no case value is matched. The default keyword is optional, but it should be used in the switch case statement.
Example: With the default KeywordThe following example demonstrates the use of the default keyword with a switch statement:
public class SwitchWithDefault { public static void main(String[] args) { int month = 13; // Example with an invalid month switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month"); } } }Output
Compile and run the above program using various command line arguments. This will produce the following result −
Invalid monthExample: Without the default Keyword
The following example demonstrates the use of the default keyword with a switch statement:
public class SwitchWithoutDefault { public static void main(String[] args) { int month = 13; // Example with an invalid month switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 12: System.out.println("December"); break; } } }Output
Compile and run the above program using various command line arguments. This will produce the following result −
NO OUTPUT
Note: Since the given month's value is 13 and we did not use the "default" statement here. Thus, nothing will be printed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4