In particular, the expression cannot be a String (although that option would be
nice to have sometimes).
The statements can be compound statements, and in a ???switch??? from other syntax, compound statements
within a switch case need not be framed in curly brackets. However, if you like curly brackets, the Java
compiler will accept curly brackets around the compound statements.
The default case is optional, but we believe a default case should always be included, if only to
provide error checking. If your program expects the days of Christmas to vary between 1 and 12, it??™s good
practice to put in a default statement which will let you know if the value of dayOfChristmas ever turns
up as some value other than 1 through 12. Especially with more complex programs, such default code could
save you many hours of debugging. Without the default statement, if none of the cases match the integral
expression, the JVM simply skips over all the switch code. Having the default case will catch the
anomalous condition and show you where the problem is.
Here is a complete program to print the words to the 12 verses of The Twelve Days of Christmas. The program
uses a while loop to iterate through the 12 verses.
Pages:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210