Java break 和 continue 语句的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/462373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between break and continue statement
提问by DonX
Can anyone tell me the difference between break
and continue
statements?
谁能告诉我break
和continue
陈述之间的区别?
采纳答案by Xn0vv3r
break
leaves a loop, continue
jumps to the next iteration.
break
离开一个循环,continue
跳转到下一次迭代。
回答by jsight
break
completely exits the loop. continue
skips the statements after the continuestatement and keeps looping.
break
完全退出循环。continue
跳过continue语句之后的语句并继续循环。
回答by Warrior
A break
statement results in the termination of the statement to which it applies (switch
, for
, do
, or while
).
一个break
语句会导致它所适用的语句终止(switch
、for
、do
、 或while
)。
A continue
statement is used to end the current loop iteration and return control to the loop statement.
一个continue
语句用来结束本次循环,并返回控制循环语句。
回答by Graeme Perrow
The break
statement breaks out of the loop (the next statement to be executed is the first one after the closing brace), while continue
starts the loop over at the next iteration.
在break
声明中,跳出循环的(下面的语句将被执行的是右括号后的第一个),同时continue
开始循环了下一轮迭代。
回答by Electrons_Ahoy
Simply put: break will terminate the current loop, and continue execution at the first line after the loop ends. continue jumps back to the loop condition and keeps running the loop.
简单的说:break 将终止当前循环,并在循环结束后在第一行继续执行。continue 跳回循环条件并继续运行循环。
回答by Gumbo
The break
statement exists the current looping control structure and jumps behind it while the continue
exits too but jumping back to the looping condition.
该break
语句存在当前循环控制结构,并在continue
退出时跳到它后面但跳回到循环条件。
回答by Jay
See Branching Statementsfor more details and code samples:
有关更多详细信息和代码示例,请参阅分支语句:
break
break
The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for, while, or do-while loop [...]
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement.
break 语句有两种形式:标记和未标记。您在前面对 switch 语句的讨论中看到了未标记的形式。您还可以使用未标记的中断来终止 for、while 或 do-while 循环 [...]
未标记的 break 语句终止最里面的 switch、for、while 或 do-while 语句,但标记的 break 终止外部语句。
continue
continue
The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]
A labeled continue statement skips the current iteration of an outer loop marked with the given label.
continue 语句跳过 for、while 或 do-while 循环的当前迭代。未标记的形式跳到最内层循环体的末尾并计算控制循环的布尔表达式。[...]
带标签的 continue 语句会跳过标有给定标签的外循环的当前迭代。
回答by Michael Buen
here's the semantic of break:
这是 break 的语义:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// find 9
for(int i = 0; i < a.Length; i++)
{
if (a[i] == 9)
goto goBreak;
Console.WriteLine(a[i].ToString());
}
goBreak:;
here's the semantic of continue:
这是 continue 的语义:
int[] a = new int[] { 1, 3, 4, 6, 7, 9, 10 };
// skip all odds
for(int i = 0; i < a.Length; i++)
{
if (a[i] % 2 == 1)
goto goContinue;
Console.WriteLine(a[i].ToString());
goContinue:;
}
回答by OscarRyz
Excellent answersimple and accurate.
优秀的答案简单而准确。
I would add a code sample.
我会添加一个代码示例。
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
回答by OscarRyz
so you are inside a for or while loop. Using break; will put you outside of the loop. As in, it will end. Continue; will tell it to run the next iteration.
所以你在 for 或 while 循环中。使用中断;会让你脱离循环。就像那样,它会结束。继续; 将告诉它运行下一次迭代。
No point in using continue in if statement, but break; is useful. In switch...case, always use break; to end a case, so it does not executes another case.
在 if 语句中使用 continue 没有意义,而是使用 break;很有用。在 switch...case 中,总是使用 break;结束一个案例,所以它不会执行另一个案例。