本帖最后由 sky_yx 于 2015-12-30 14:10 编辑
java break 用法之当goto用 break一般用来跳出当前循环: public class Break { public static void main(String args[]) { for (int i = 0; i<3; i++){ System. out .println("i = " + i); for (int j = 0;j<100;j++){ if (j == 8) break; System. out .print(" j = " +j); } System. out .println("In signal 1" ); } System. out .println("out Loops " ); }
如果我们想实现break跳出所有循环呢:
public class Break { public static void main(String args[]) { boolean sig = false; signal1: for (int i = 0; i < 3; i++) { System. out .println("i = " + i); for (int j = 0; j < 100; j++) { if (j == 8) break signal1; System. out .print(" j = " + j); } sig = true ; System. out .println("In signal 1" ); } System. out .println("out Loops " ); if (sig == true) { System. out .println("外层循环未被break" ); } else System. out .println("所有循环break" ); } break 标签;break 跳出制定块,可以是任何块{},但是块内必须包含break 标签;
|