什么是“ continue”关键字,它在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? 什么是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语句将从最里面的whiledo循环的条件以及最里面的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: 相反,考虑它们会更容易:

  • break terminates the loop (jumps to the code below it). break终止循环(跳至其下面的代码)。

  • continue terminates the rest of the processing of the code within the loop for the current iteration, but continues the loop. 对于当前迭代, 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. 这只会打印出奇数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值