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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.