java - switch case statement fall through and b...

In this post, I will lead the review on the java syntax where I will show you how to do the label fall through and break outer - a cool feature that allow you to break out of a loop prematurely. Here is the code.
public class SwtichCaseStatements {
    /**
     * @param args
     */
    public static void main(String[] args) {
           
        outer:
        for (int i = 0; i < 5; i++)
        {
          int step = i;
          out.println("=====================");
          out.format("Step = %s\n", step);

          switch (step)
          {
          case 3:
              out.println(3); // you can break outer, so in this case, if you hit 1, then yo will break out of the for loop
              break outer;
          case 2:
              out.println(2); 
          case 1:
              out.println(1); 
          case 0:
              out.println(0);
              break;
          default:
              out.println("...");
              break;
              
          }
        }    
      }
}

你可能感兴趣的:(java)