Notice the break statements. The break statement says to exit the switch statement. If the break
statement is missing, the execution of the switch statement will ???fall through??? to the next case. Sometimes
you will want that to happen, but certainly not always. Forgetting to insert the break statement appropriately
is a common programming error.
Here??™s an example using the switch statement to allow ???falling through??? from one condition to the next
in a helpful way:
switch( dayOfChristmas ) {
case 12: System.out.println( "Twelve drummers drumming" );
case 11: System.out.println( "Eleven pipers piping" );
case 10: System.out.println( "Ten lords a-leaping" );
case 9: System.out.println( "Nine ladies dancing" );
case 8: System.out.println( "Eight maids a-milking" );
case 7: System.out.println( "Seven swans a-swimming" );
case 6: System.out.println( "Six geese a-laying" );
case 5: System.out.println( "Five golden rings" );
case 4: System.out.println( "Four calling birds" );
case 3: System.out.println( "Three French hens" );
case 2: System.out.println( "Two turtle doves" );
case 1: System.out.println( "And a partridge ... tree" );
break;
default: System.out.println( "?Day = " + dayOfChristmas );
}
Notice that in this example, the case statements proceed from highest to lowest, instead of the other way
around.
Pages:
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208