在Java中打破for循环

``public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } } Outputs: value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19` `

` `public class Test { public static void main(String args[]) { boolean breakLoop = false; while (!breakLoop) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); if (x = 15) { breakLoop = true; } } } } }` `

` `public class Test { public static void main(String args[]) { breakLoop: for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); if (x = 15) { break breakLoop; } } } }` `

`break;` 是什么你需要摆脱任何循环语句像， `while``do-while`

` `for(int x = 10; x < 20; x++) { // The below condition can be present before or after your sysouts, depending on your needs. if(x == 15){ break; // A unlabeled break is enough. You don't need a labeled break here. } System.out.print("value of x : " + x ); System.out.print("\n"); }` `

` `for (int x = 0; x < 10; x++) { if (x == 5) { // If x is 5, then break it. break; } }` `

` `boolean test = true; for (int i = 0; i < 1220 && test; i++) { System.out.println(i); if (i == 20) { test = false; } }` `

for循环的第二个参数是一个布尔testing。 如果testing结果为真，循环将停止。 如果你喜欢，你可以使用不仅仅是一个简单的mathtesting。 否则，一个简单的rest也可以做到这一点，正如其他人所说：

` `for (int i = 0; i < 1220 ; i++) { System.out.println(i); if (i == 20) { break; } }` `
` `public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { if(x==15) break; System.out.print("value of x : " + x ); System.out.print("\n"); } } }` `

` `for(int k=0;k<10;k=k+2) { if(k==2) { break; } System.out.println(k); }` `

` `myloop: for (int i=0; i < 5; i++) { for (int j=0; j < 5; j++) { if (i * j > 6) { System.out.println("Breaking"); break myloop; } System.out.println(i + " " + j); } }` `