Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0050 The break statement is optional

If you omit the break, execution will continue on into the next case. For example, consider the following program: public class Main { public static void main(String args[]) { for (int i = 0; i < 12; i++) switch (i) { case 0: case 1: case 2: case 3: case 4: System.out.println("i is less than 5"); break; case 5: case 6: case 7: case 8: case 9: System.out.println("i is less than 10"); break; default: System.out.println("i is 10 or more"); } } } This program generates the following output: i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is 10 or more i is 10 or more