switch statement in Java



A java switch statement is an alternative to a neste if else if else .... structure. Instead of checking the value of an expression at each if statement, we use a switch statemet to check it once and then jump to of of the labels that matches its value. Let us a take a look at the following example that attempts to print number of days in a month of a year.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding a switch statement
  4.   */
  5.  
  6. class switch1{
  7. public static void main (String args[]) {
  8. int monthnumber = 3 ;
  9. int year = 2013;
  10. int numberofdays = 0;
  11. switch (monthnumber)
  12. {
  13. case 1 :
  14. numberofdays = 31; // January
  15. break;
  16. case 2 : // February
  17. if (year % 4 == 0)
  18. numberofdays = 29;
  19. else numberofdays = 29;
  20. break;
  21. case 3 : // March
  22. numberofdays = 31;
  23. break;
  24. case 4 : // April
  25. numberofdays = 30;
  26. break;
  27. case 5 : // May
  28. numberofdays = 31;
  29. break;
  30. case 6 : // June
  31. numberofdays = 30;
  32. break;
  33. case 7 : // July
  34. numberofdays = 31;
  35. break;
  36. case 8 : // Aug
  37. numberofdays = 31;
  38. break;
  39. case 9 : // Sep
  40. numberofdays = 30;
  41. break;
  42. case 10 : // Oct
  43. numberofdays = 31;
  44. break;
  45. case 11: // Nov
  46. numberofdays = 30;
  47. break;
  48. case 12 : // Dec
  49. numberofdays = 31;
  50. break;
  51. default :
  52. numberofdays = 31; // Default number of days
  53. break;
  54.  
  55.  
  56. }
  57.  
  58. System.out.println("Number of days in month "+ monthnumber +" is " + numberofdays);
  59. }
  60. }
  61.  


If you compile and run, you get the following output


C:\Program Files\Java\jdk1.7.0_17\bin>java switch1
Number of days in month 3 is 31




Let us formally define a switch contruct.

  1. switch ( variable_to_test ) {
  2. case value:
  3. code_here;
  4. break;
  5. case value:
  6. code_here;
  7. break;
  8. default:
  9. values_not_caught_above;
  10.  
  11. }

The variable variable_to_test that we want to check resides between the round brackets of switch. For every value that we want to check, we use the keyword case. You then have the value you want to check for:

case value:

After case value comes a colon. You then put what you want to happen if the value matches. This is your code that you want executed. The keyword break is needed to break out of each case of the switch statement.

The default value at the end is optional. It can be included if there are other values that can be held in your variable but that you haven't checked for elsewhere in the switch statement.

Special Considerations

1. If you do not include a break statement, the program continues to execute the statements next to it. It can be a bug or a feature depending upon how you use it. For example, consider the following example where we missed the break statement from case 18.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding use of break in switch statement
  4.   */
  5.  
  6. class switch2{
  7. public static void main (String args[]) {
  8. int age = 18 ;
  9. switch (age)
  10. {
  11. case 18 :
  12. System.out.println("You are "+ age +" years old " );
  13. case 19 :
  14. System.out.println("You are "+ age +" years old " );
  15. break;
  16. case 20 :
  17. System.out.println("You are "+ age +" years old " );
  18. break;
  19. default :
  20. System.out.println("You are more than 20 or less than 18 years old " );
  21. break;
  22. }
  23. }
  24. }


You get the following output


You are  18 years old
You are  18 years old




You get program first transfers the control to case 18 and executes the statement

System.out.println("You are "+ age +" years old " );

Since there is no break statement, it continues to execute the statement(s) next to case 19, and the statement is printed again.

The omission of the break statement can be used favorably. Consider the number of days in a month example where use the absence of break statement favorably.



  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding a switch statement - absence of break
  4.   */
  5.  
  6. class switch3{
  7. public static void main (String args[]) {
  8. int monthnumber = 6 ;
  9. int year = 2013;
  10. int numberofdays = 0;
  11. switch (monthnumber)
  12. {
  13. case 4 : // April
  14. case 6 : // June
  15. case 9 : // Sept
  16. case 11 : // Nov
  17. numberofdays = 30;
  18. break;
  19. case 1 : // Jan
  20. case 3 : // Mar
  21. case 5 : // May
  22. case 7 : // July
  23. case 8 : // Aug
  24. case 10 : // Oct
  25. case 12 : // Dec
  26. numberofdays = 31;
  27. break;
  28. case 2 : // February
  29. if (year % 4 == 0)
  30. numberofdays = 29;
  31. else numberofdays = 29;
  32. break;
  33. default :
  34. break;
  35.  
  36. }
  37.  
  38. System.out.println("Number of days in month "+ monthnumber +" is " + numberofdays);
  39. }
  40. }
  41.  


You get the following output


Number of days in month 6 is 30




The value of the variable monthnumber transfers the control to

case 6:

There are no statements to execute and since, there is no break statement, the flow continues to execute the next case statement which is case 9:. There is no statement to execute in case 9 as well and the control contines to execute next case - which is case 11. It assigns 30 to numberofdays 30 and the control breaks.

See how the absence of break has been used favorably.

Using default to our advantage



In the number of days in a month example, we can simplify it even more by making use of the deafault. Assuming that the variable monthnumber always takes a valid value between 1 ( corresponding to January) and 12 ( corresponding to December), we can simplify the program as follows.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding a switch statement - the default
  4.   */
  5.  
  6. class switch4{
  7. public static void main (String args[]) {
  8. int monthnumber = 8 ; // should be between 1 to 12
  9. int year = 2013;
  10. int numberofdays = 0;
  11. switch (monthnumber)
  12. {
  13. case 4 : // April
  14. case 6 : // June
  15. case 9 : // Sept
  16. case 11 : // Nov
  17. numberofdays = 30;
  18. break;
  19. case 2 : // February
  20. if (year % 4 == 0)
  21. numberofdays = 29;
  22. else numberofdays = 29;
  23. break;
  24. default :
  25. numberofdays = 31;
  26. break;
  27.  
  28. }
  29.  
  30. System.out.println("Number of days in month "+ monthnumber +" is " + numberofdays);
  31. }
  32. }
  33.