Unlikeif-thenandif-then-else, theswitchstatement allows for any number of possible execution paths. Aswitchworks with thebyte,short,char, andintprimitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types:Character,Byte,Short, andInteger(discussed in Simple Data Objects ).The following program,
SwitchDemo, declares anintnamedmonthwhose value represents a month out of the year. The program displays the name of the month, based on the value of month, using theswitchstatement.class SwitchDemo { public static void main(String[] args) { int month = 8; 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 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break; } } }In this case, "August" is printed to standard output.
The body of a
switchstatement is known as a switch block. Any statement immediately contained by theswitchblock may be labeled with one or morecaseordefaultlabels. Theswitchstatement evaluates its expression and executes the appropriatecase.Of course, you could also implement the same thing with
if-then-elsestatements:Deciding whether to useint month = 8; if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } . . . // and so onif-then-elsestatements or aswitchstatement is sometimes a judgment call. You can decide which one to use based on readability and other factors. Anif-then-elsestatement can be used to make decisions based on ranges of values or conditions, whereas aswitchstatement can make decisions based only on a single integer or enumerated value.Another point of interest is the
breakstatement after eachcase. Eachbreakstatement terminates the enclosingswitchstatement. Control flow continues with the first statement following theswitchblock. Thebreakstatements are necessary because without them,casestatements fall through; that is, without an explicitbreak, control will flow sequentially through subsequentcasestatements. The following program,SwitchDemo2, illustrates why it might be useful to havecasestatements fall through:class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays); } }This is the output from the program.
Number of Days = 29Technically, the final
breakis not required because flow would fall out of theswitchstatement anyway. However, we recommend using abreakso that modifying the code is easier and less error-prone. Thedefaultsection handles all values that aren't explicitly handled by one of thecasesections.