什么是“继续”关键字以及它如何在Java中运行? [英] What is the "continue" keyword and how does it work in Java?
问题描述
我第一次看到这个关键字,我想知道是否有人可以向我解释它的作用。
I saw this keyword for the first time and I was wondering if someone could explain to me what it does.
- 什么是
继续
关键字? - 它是如何运作的?
- 何时使用?
- What is the
continue
keyword? - How does it work?
- When is it used?
推荐答案
没有标签的继续
声明将从最里面的条件重新执行,而
或执行
循环,并从最里面的<$的更新表达式中重新执行c $ c> for 循环。它通常用于提前终止循环的处理,从而避免深度嵌套的 if
语句。在以下示例中, continue
将获取下一行,而不在循环中处理以下语句。
A continue
statement without a label will re-execute from the condition the innermost while
or do
loop, and from the update expression of the innermost for
loop. It is often used to early-terminate a loop's processing and thereby avoid deeply-nested if
statements. In the following example continue
will get the next line, without processing the following statement in the loop.
while (getNext(line)) {
if (line.isEmpty() || line.isComment())
continue;
// More code here
}
带标签, continue
将从具有相应标签的循环重新执行,而不是最内层循环。这可以用于逃避深层嵌套循环,或者仅为了清楚起见。
With a label, continue
will re-execute from the loop with the corresponding label, rather than the innermost loop. This can be used to escape deeply-nested loops, or simply for clarity.
有时 continue
也用作占位符,以使空循环体更清晰。
Sometimes continue
is also used as a placeholder in order to make an empty loop body more clear.
for (count = 0; foo.moreData(); count++)
continue;
C和C ++中也存在没有标签的相同语句。 Perl中的等价物是 next
。
The same statement without a label also exists in C and C++. The equivalent in Perl is next
.
不推荐这种类型的控制流程,但是如果你选择了也可以使用 continue
来模拟有限形式的 goto
。在以下示例中, continue
将为(;;)循环重新执行空。
This type of control flow is not recommended, but if you so choose you can also use continue
to simulate a limited form of goto
. In the following example the continue
will re-execute the empty for (;;)
loop.
aLoopName: for (;;) {
// ...
while (someCondition)
// ...
if (otherCondition)
continue aLoopName;
这篇关于什么是“继续”关键字以及它如何在Java中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!