Thinking in JAVA label for ,similar to go to.
时间:2010-08-26 来源:jrvin
public class LabeledFor { public static void main(String[] args) { int i = 0; outer: // Can’t have statements here for (; i<10;i++) { // infinite loop inner: // Can’t have statements here for (; i < 10; i++) { out.print("i = " + i); if (i == 2) { out.println("continue"); continue; } if (i == 3) { out.println("break"); i++; // Otherwise i never // gets incremented. break; } if (i == 7) { out.println("continue outer"); i++; // Otherwise i never // gets incremented. continue outer; } if (i == 8) { out.println("break outer"); break outer; } for (int k = 0; k < 5; k++) { if (k == 3) { out.println("continue inner"); continue inner; } } } } // Can’t break or continue to labels here } }
Note that break breaks out of the for loop, and that the increment expression doesn’t occur until the end of the pass through the for loop. Since break skips the increment expression, the increment is performed directly in the case of i == 3. The continue outer statement in the case of i == 7 also goes to the top of the loop and also skips the increment, so it too is incremented directly.
continue outer .相当于在最外面那个循环里面调用continue.里面的i值还是要增加的。
continue inner;相当于在里面那个循环里面调用continue;里面的i值也是要增加的。
相关阅读 更多 +