In this example, I will show you how to use switch case statement with a few examples. Before examples let me explain what is the switch statement.
What is Switch statement
The switch statement evaluates an expression, matching the expression’s value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered. The default clause of a switch statement will be jumped to if no case matches the expression’s value.
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
switch(expression) { case 1: // code block break; case 2: // code block break; case 3: // code block break; default: // code block } |
Switch Case Examples in Java
Example 1: Enter the favorite color and print answer.(Java String Example)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Main { public static void main(String[] args) { String favColor = "green"; switch(favColor){ case "blue": System.out.println("Your favorite color is BLUE"); break; case "red": System.out.println("Your favorite color is RED"); break; case "green": System.out.println("Your favorite color is GREEN"); break; default: System.out.println("No matced favorite color"); break; } } } |
Output:
1 2 3 |
Your favorite color is GREEN |
Example 2: Switch example with Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; // Import the Scanner class class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter your favorite color : "); String favColor = scanner.nextLine(); switch(favColor){ case "blue": System.out.println("Your favorite color is BLUE"); break; case "red": System.out.println("Your favorite color is RED"); break; case "green": System.out.println("Your favorite color is GREEN"); break; default: System.out.println("No matced favorite color"); break; } } } |
Output:
1 2 3 4 |
Enter your favorite color : red Your favorite color is RED |
Example 3 Enter month number then show month name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Main { public static void main(String[] args) { int month = 3; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } } |
Output:
1 2 3 |
March |
Example 4: Java switch case multiple values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; // Import the Scanner class class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter month number : "); int monthNumber = scanner.nextInt(); switch(monthNumber){ case 12: case 1: case 2: System.out.println("Winter"); break; case 3: case 4: case 5: System.out.println("Spring"); break; case 6: case 7: case 8: System.out.println("Summer"); break; case 9: case 10: case 11: System.out.println("Fall"); break; default: System.out.println("Wrong number. Enter between 1-12"); break; } } } |
Output:
1 2 3 4 |
Enter month number : 6 Summer |
Example 5: Switch case with Enum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Main { public static void main(String[] args) { Day day = Day.SATURDAY; System.out.print( getAnswer(day) ); } public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public static String getAnswer(Day day){ switch(day){ case MONDAY: case TUESDAY: case WEDNESDAY: case THURSDAY: case FRIDAY: return "Work is so hard"; case SUNDAY: case SATURDAY: return "Weekend is the best"; default: return "Some thing went wrong :("; } } } |
Output:
1 2 3 |
Weekend is the best |
Example 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class Main { public static void main(String[] args) { /* * Switch statement starts here. Added 10 cases and * one default statement. Execution will flow through * each of these cases case 0 to case 4 and case 5 to * case 9 until it finds a break statement. */ for(int i=0; i<=10; i++) { switch(i){ case 0: case 1: case 2: case 3: case 4: System.out.println("i value is less than 5"); break; case 5: case 6: case 7: case 8: case 9: System.out.println("i value is less than 10"); break; default: System.out.println("Default statement"); } } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
i value is less than 5 i value is less than 5 i value is less than 5 i value is less than 5 i value is less than 5 i value is less than 10 i value is less than 10 i value is less than 10 i value is less than 10 i value is less than 10 Default statement |