本文翻译自: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
keyword? 什么是continue
关键字?continue
- How does it work? 它是如何工作的?
- When is it used? 什么时候使用?
#1楼
参考:https://stackoom.com/question/1dO9/什么是-continue-关键字-它在Java中如何工作
#2楼
As already mentioned
continue
will skip processing the code below it and until the end of the loop.
如前所述,continue
将跳过对它下面的代码的处理,直到循环结束。 Then, you are moved to the loop's condition and run the next iteration if this condition still holds (or if there is a flag, to the denoted loop's condition).
然后,将您移至循环的条件 ,如果该条件仍然成立(或如果存在标志,则为指定的循环的条件) ,则运行下一个迭代。It must be highlighted that in the case of
do - while
you are moved to the condition at the bottom after a
continue
, not at the beginning of the loop.
必须强调的是,在do - while
的情况下 do - while
您在 continue
之后而不是循环开始时移至底部的条件时。 This is why a lot of people fail to correctly answer what the following code will generate.
这就是为什么许多人无法正确回答以下代码将生成什么的原因。Random r = new Random();
Set<Integer> aSet= new HashSet<Integer>();
int anInt;
do {
anInt = r.nextInt(10);
if (anInt % 2 == 0)
continue;
System.out.println(anInt);
} while (aSet.add(anInt));
System.out.println(aSet);
*If your answer is that
aSet
will contain odd numbers only 100%... you are wrong!
*如果您的答案是aSet
仅包含100%的奇数...您错了! #3楼
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.
没有标签的continue
语句将从最里面的 while
或 do
循环的条件以及最里面的 for
循环的更新表达式重新执行。 It is often used to early-terminate a loop's processing and thereby avoid deeply-nested
if
statements.
它通常用于尽早终止循环的处理,从而避免深层嵌套if
语句。 In the following example
continue
will get the next line, without processing the following statement in the loop.
在下面的示例中,continue
将获得下一行,而不在循环中处理以下语句。 while (getNext(line)) {
if (line.isEmpty() || line.isComment())
continue;
// More code here
}
With a label,
continue
will re-execute from the loop with the corresponding label, rather than the innermost loop.
使用标签时,continue
将使用相应的标签而不是最内部的循环从循环中重新执行。 This can be used to escape deeply-nested loops, or simply for clarity.
这可用于逃避深度嵌套的循环,或者只是为了清楚起见。Sometimes
continue
is also used as a placeholder in order to make an empty loop body more clear.
有时,continue
还用作占位符,以使空循环主体更清晰。 for (count = 0; foo.moreData(); count++)
continue;
The same statement without a label also exists in C and C++.
C和C ++中也存在不带标签的相同语句。The equivalent in Perl is
next
.
Perl中的等效项是next
。 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
.
不建议使用这种类型的控制流,但是如果您选择这样做,也可以使用continue
模拟有限形式的 goto
。 In the following example the
continue
will re-execute the empty
for (;;)
loop.
在下面的示例中,continue
将重新执行空的 for (;;)
循环。 aLoopName: for (;;) {
// ...
while (someCondition)
// ...
if (otherCondition)
continue aLoopName;
#4楼
continue
is kind of like
goto
.
continue
有点像 goto
。 Are you familiar with
break
?
您对break
熟悉吗? It's easier to think about them in contrast:
相反,考虑它们会更容易:-
terminates the loop (jumps to the code below it).break
终止循环(跳至其下面的代码)。break
-
terminates the rest of the processing of the code within the loop for the current iteration, but continues the loop. 对于当前迭代,continue
终止循环中其余代码的处理,但继续循环。continue
#5楼
Let's see an example:
让我们来看一个例子:int sum = 0;
for(int i = 1; i <= 100 ; i++){
if(i % 2 == 0)
continue;
sum += i;
}
This would get the sum of only odd numbers from 1 to 100.
这将得到从1到100的仅奇数之和。#6楼
If you think of the body of a loop as a subroutine,
continue
is sort of like
return
.
如果您将循环的主体视为子程序,则continue
类似于 return
。 The same keyword exists in C, and serves the same purpose.
C中存在相同的关键字,并且具有相同的用途。Here's a contrived example:
这是一个人为的示例:for(int i=0; i < 10; ++i) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
This will print out only the odd numbers.
这只会打印出奇数。