Xn0*_*v3r 511
break留下一个循环,continue跳转到下一个迭代.
Jay*_*Jay 99
有关更多详细信息和代码示例,请参阅分支语句:
breakbreak语句有两种形式:标记和未标记.您在前面的switch语句讨论中看到了未标记的表单.您还可以使用未标记的中断来终止for,while或do-while循环[...]
未标记的break语句终止最内层的switch,for,while或do-while语句,但带标签的break终止外部语句.
continuecontinue语句跳过for,while或do-while循环的当前迭代.未标记的表单跳到最内层循环体的末尾,并计算控制循环的布尔表达式.[...]
标记的continue语句跳过标记有给定标签的外循环的当前迭代.
use*_*own 63
System.out.println ("starting loop:");
for (int n = 0; n < 7; ++n)
{
System.out.println ("in loop: " + n);
if (n == 2) {
continue;
}
System.out.println (" survived first guard");
if (n == 4) {
break;
}
System.out.println (" survived second guard");
// continue at head of loop
}
// break out of loop
System.out.println ("end of loop or exit via break");
Run Code Online (Sandbox Code Playgroud)
这将导致以下输出:
starting loop:
in loop: 0
survived first guard
survived second guard
in loop: 1
survived first guard
survived second guard
in loop: 2
in loop: 3
survived first guard
survived second guard
in loop: 4
survived first guard
end of loop or exit via break
Run Code Online (Sandbox Code Playgroud)
您可以标记块,而不仅仅是for循环,然后从嵌套块中断/继续到外层块.在少数情况下,这可能很有用,但一般情况下,您会尝试避免使用此类代码,除非程序的逻辑比以下示例更好理解:
first:
for (int i = 0; i < 4; ++i)
{
second:
for (int j = 0; j < 4; ++j)
{
third:
for (int k = 0; k < 4; ++k)
{
System.out.println ("inner start: i+j+k " + (i + j + k));
if (i + j + k == 5)
continue third;
if (i + j + k == 7)
continue second;
if (i + j + k == 8)
break second;
if (i + j + k == 9)
break first;
System.out.println ("inner stop: i+j+k " + (i + j + k));
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为它是可能的,但这并不意味着你应该使用它.
如果你想以一种有趣的方式混淆你的代码,你不要选择一个有意义的名字,而是选择http:并使用注释来跟随它,它看起来很陌生,就像源代码中的webadress一样:
http://stackoverflow.com/questions/462373
for (int i = 0; i < 4; ++i)
{
if (i == 2)
break http;
Run Code Online (Sandbox Code Playgroud)
我猜这是来自约书亚布洛赫的一个quizzle.:)
小智 28
Break完全离开循环并在循环后执行语句.然后,Continue离开当前迭代并使用循环中的下一个值执行.
本规范解释了一切:
public static void main(String[] args) {
for(int i=0;i<10;i++)
{
if (i==4)
{
break;
}
System.out.print(i+"\t");
}
System.out.println();
for(int i=0;i<10;i++)
{
if (i==4)
{
continue;
}
System.out.print(i+"\t");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0 1 2 3
0 1 2 3 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
gom*_*sha 11
休息声明
有时需要在循环完全迭代所有步长值之前退出循环.例如,循环遍历数字列表,直到找到满足某个条件的数字.或者循环文件中的字符流直到读取某个字符.
在下面的示例中,我们使用一个简单的for循环来打印0到9之间的值:
for(int i=0; i<10; i++) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
1
2
3
4
5
6
7
8
9
Run Code Online (Sandbox Code Playgroud)
现在,如果我们在i == 4时添加一个break语句,一旦我等于4,我们的代码就会突破循环.你可以使用break语句来打破for循环,while循环和do-while循环.break语句只会突破当前循环.为了从嵌套的内部循环中突破外部循环,您需要使用带有break语句的标签.
for(int i=0; i<10; i++) {
System.out.println(i);
if(i==4) {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
继续声明
Java的continue语句跳过循环的当前迭代并直接进入下一次迭代.在for循环中调用continue语句之后,循环执行将执行步骤值并在继续下一次迭代之前评估布尔条件.在下面的示例中,我们在循环中打印出0到9之间的所有值,但是我们跳过打印出4.
for(int i=0; i<10; i++) {
if(i==4) {
continue;
}
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
1
2
3
5 <---- SKIPPED OVER 4 and continued with next loop iteration
6
7
8
9
Run Code Online (Sandbox Code Playgroud)
循环标签 - 中断语句通过指定在中断循环后要执行的位置, 可以在嵌套循环中使用标签.通常,break语句只会从最内层循环中断开,所以当你想要打破外部循环时,你可以使用标签来完成这个,基本上做类似于goto语句的东西.
以下示例使用3个循环,所有循环都嵌套在一起.由于无法完全从最内层循环内部中断出最外层循环,因此我们可以使用标签"outer1"来完成此操作并指定break语句旁边的标签.
outer1:
for(int i=0; i<5; i++) {
for(int j=0; j<4; j++) {
for(int k=0; k<2; k++) {
System.out.println("[" + i + "][" + j + "][" + k + "]");
if(j == 3) {
break outer1;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0]
Run Code Online (Sandbox Code Playgroud)
注意显示的最后一行是" 0 [0]",这是j == 3的地方,也就是我们称之为"break outer1;"以突破最外面的循环.
循环标签 - 继续声明
您还可以使用带有continue关键字的标签继续从特定点循环.采用前面的示例并只更改一行来指定continue outer1;而不是break outer1;将导致循环继续从outer1标签循环而不是退出循环.注意每次continue outer1;调用时,代码在将循环索引i递增1之后从外部循环继续.
outer1:
for(int i=0; i<5; i++) {
for(int j=0; j<4; j++) {
for(int k=0; k<2; k++) {
System.out.println("[" + i + "][" + j + "][" + k + "]");
if(j == 3) {
continue outer1;
}
}
}
[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[1][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[1][0][1]
[1][1][0]
[1][1][1]
[1][2][0]
[1][2][1]
[1][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[2][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[2][0][1]
[2][1][0]
[2][1][1]
[2][2][0]
[2][2][1]
[2][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[3][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[3][0][1]
[3][1][0]
[3][1][1]
[3][2][0]
[3][2][1]
[3][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[4][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[4][0][1]
[4][1][0]
[4][1][1]
[4][2][0]
[4][2][1]
[4][3][0]
Run Code Online (Sandbox Code Playgroud)
优秀的答案简单而准确.
我会添加一个代码示例.
C:\oreyes\samples\java\breakcontinue>type BreakContinue.java
class BreakContinue {
public static void main( String [] args ) {
for( int i = 0 ; i < 10 ; i++ ) {
if( i % 2 == 0) { // if pair, will jump
continue; // don't go to "System.out.print" below.
}
System.out.println("The number is " + i );
if( i == 7 ) {
break; // will end the execution, 8,9 wont be processed
}
}
}
}
C:\oreyes\samples\java\breakcontinue>java BreakContinue
The number is 1
The number is 3
The number is 5
The number is 7
Run Code Online (Sandbox Code Playgroud)
continue跳过当前执行的循环和移动到下一个循环,而break 移出了的循环和执行下一条语句后循环.我使用以下代码了解了差异.查看不同的输出.希望这有帮助.
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
continue;
}
System.out.print(i);
}
}//prints out 0124, continue moves to the next iteration skipping printing 3
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
break;
}
System.out.print(i);
}
}//prints out 012, break moves out of the loop hence doesnt print 3 and 4
Run Code Online (Sandbox Code Playgroud)