1. Overview
1.概述
In this quick article, we’ll introduce continue and break Java keywords and focus on how to use them in practice.
在这篇快速文章中,我们将介绍continue和break这两个Java关键字,并重点介绍如何在实践中使用它们。
Simply put, execution of these statements causes branching of the current control flow and terminates the execution of the code in the current iteration.
简单地说,执行这些语句会导致当前控制流的分支,并终止当前迭代中代码的执行。
2. The break Statement
2.突破声明
The break statement comes in two forms: unlabeled and labeled.
break语句有两种形式:无标签和有标签。
2.1. Unlabeled break
2.1.无标记的突破
We can use the unlabeled statement to terminate a for, while or do-while loop as well as the switch-case block:
我们可以使用无标签语句来终止for、while或do-while循环,以及switch-case块。
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
}
This snippet defines a for loop that is supposed to iterate five times. But when counter equals 3, the if condition becomes true and the break statement terminates the loop. This causes the control flow to be transferred to the statement that follows after the end of for loop.
这个片段定义了一个for循环,它应该迭代五次。但当counter等于3时,if条件变为true,break语句终止循环。这导致控制流被转移到for循环结束后的语句。
In case of nested loops, an unlabeled break statement only terminates the inner loop that it’s in. Outer loops continue execution:
在嵌套循环的情况下,一个没有标记的break语句只终止它所在的内循环。外循环继续执行。
for (int rowNum = 0; rowNum < 3; rowNum++) {
for (int colNum = 0; colNum < 4; colNum++) {
if (colNum == 3) {
break;
}
}
}
This snippet has nested for loops. When colNum equals 3, the if the condition evaluates to true and the break statement causes the inner for loop to terminate. However, the outer for loop continues iterating.
这个片段有嵌套的for循环。当colNum等于3时,if条件评估为true,break语句导致内部for循环终止。然而,外部for循环继续迭代。
2.2. Labeled break
2.2.标记的突破
We can also use a labeled break statement to terminate a for, while or do-while loop. A labeled break terminates the outer loop.
我们也可以使用标记的break语句来终止for、while或do-while循环。带标签的break可以终止外循环。
Upon termination, the control flow is transferred to the statement immediately after the end of the outer loop:
终止时,控制流被转移到紧随外循环结束的语句。
compare:
for (int rowNum = 0; rowNum < 3; rowNum++) {
for (int colNum = 0; colNum < 4; colNum++) {
if (rowNum == 1 && colNum == 3) {
break compare;
}
}
}
In this example, we introduced a label just before the outer loop. When rowNum equals 1 and colNum equals 3, the if condition evaluates to true and the break statement terminates the outer loop.
在这个例子中,我们在外循环之前引入了一个标签。当rowNum等于1,colNum等于3时,if条件评估为true,break语句终止外循环。
The control flow is then transferred to the statement following the end of outer for loop.
然后控制流被转移到外层for循环结束后的语句。
3. The continue Statement
3.继续声明
The continue statement also comes in two forms: unlabeled and labeled.
continue语句也有两种形式:无标签和有标签。
3.1. Unlabeled continue
3.1.无标签的继续
We can use an unlabeled statement to bypass the execution of rest of the statements in the current iteration of a for, while or do-while loop. It skips to the end of the inner loop and continues the loop:
我们可以使用一个无标记的语句来绕过for、while或do-while循环的当前迭代中的其他语句的执行。它跳到内循环的末端并继续循环。
int counter = 0;
for (int rowNum = 0; rowNum < 3; rowNum++) {
for (int colNum = 0; colNum < 4; colNum++) {
if (colNum != 3) {
continue;
}
counter++;
}
}
In this snippet, whenever colNum is not equal to 3, the unlabeled continue statement skips the current iteration, thus bypassing the increment of the variable counter in that iteration. However, the outer for loop continues to iterate. So, the increment of counter happens only when colNum equals 3 in each iteration of the outer for loop.
在这个片段中,只要colNum不等于3,无标签的continue语句就会跳过当前迭代,从而绕过该迭代中变量counter的增量。然而,外部for循环继续迭代。所以,只有当colNum等于3时,counter才会在外层for循环的每次迭代中发生递增。
3.2. Labeled continue
3.2.标记的继续
We can also use a labeled continue statement which skips the outer loop. Upon skipping, the control flow is transferred to the end of the outer loop, effectively continuing the iteration of the outer loop:
我们还可以使用标记的continue语句来跳过外循环。跳过后,控制流被转移到外循环的末端,有效地继续外循环的迭代。
int counter = 0;
compare:
for (int rowNum = 0; rowNum < 3; rowNum++) {
for (int colNum = 0; colNum < 4; colNum++) {
if (colNum == 3) {
counter++;
continue compare;
}
}
}
We introduced a label just before the outer loop. Whenever colNum equals 3, the variable counter is incremented. The labeled continue statement causes the iteration of outer for loop to skip.
我们在外循环之前引入了一个标签。每当colNum等于3时,变量counter被递增。标记的continue语句使外循环for的迭代跳过。
The control flow is transferred to the end of the outer for loop, which continues with the next iteration.
控制流被转移到外部for循环的末端,继续进行下一次迭代。
4. Conclusion
4.结论
In this tutorial, we’ve seen different ways of using the keywords break and continue as branching statements in Java.
在本教程中,我们已经看到了在Java中使用关键字break和continue作为分支语句的不同方式。
The complete code presented in this article is available over on GitHub.
本文介绍的完整代码可在GitHub上获得over。