|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Use the switchstatement to conditionally perform statements based on an integer expression. Following is a sample program,
SwitchDemo, that declares an integer named
monthwhose value supposedly represents the month in a date. The program displays the name of the month, based on the value ofmonth, using theswitchstatement:Thepublic 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; } } }switchstatement evaluates its expression, in this case the value ofmonth, and executes the appropriate casestatement. Thus, the output of the program is:
August. Of course, you could implement this by using anifstatement:Deciding whether to use anint month = 8; if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } . . . // and so onifstatement or aswitchstatement is a judgment call. You can decide which to use, based on readability and other factors. Anifstatement can be used to make decisions based on ranges of values or conditions, whereas aswitchstatement can make decisions based only on a single integer value. Also, the value provided to eachcasestatement must be unique.Another point of interest in the
switchstatement is the breakstatement after each
case. Eachbreakstatement terminates the enclosingswitchstatement, and the flow of control continues with the first statement following theswitchblock. Thebreakstatements are necessary because without them, thecasestatements fall through. That is, without an explicitbreak, control will flow sequentially through subsequentcasestatements. Following is an example,SwitchDemo2, which illustrates why it might be useful to have
casestatements fall through:The output from this program is:public 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; } System.out.println("Number of Days = " + numDays); } }Technically, the finalNumber of Days = 29breakis not required because flow would fall out of theswitchstatement anyway. However, we recommend using abreakfor the lastcasestatement just in case you need to add morecasestatements at a later date. This makes modifying the code easier and less error-prone.You will see
breakused to terminate loops in Branching Statements.
Finally, you can use the default
statement at the end of the
switchto handle all values that aren't explicitly handled by one of thecasestatements.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("Hey, that's not a valid month!"); break; }
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.