C# 如何在while循环中使用continue语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14386679/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:40:45  来源:igfitidea点击:

How to use the statement continue in while loop?

c#javawhile-loopcontinue

提问by Code Enthusiastic

Lets take the below code snippet:

让我们看下面的代码片段:

int i = 0;
while ( i <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }
    i++;
}

What changes do I have to make in the code to avoid infinite loop?

我必须在代码中进行哪些更改才能避免无限循环?

采纳答案by Eric Petroelje

Do your increment at the beginning instead of at the end:

在开始而不是在结束时进行增量:

int i = -1;
while ( i <= 10 )
{
    i++;
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }

    // Presumably there would be some code here, or this doesn't really make much sense
}

Or, depending on the language, you could do it right in the whilestatement (keeping in mind operator precedence in whether you choose i++or ++i)

或者,根据语言,您可以在while语句中正确执行(请记住,无论您选择i++还是 ,运算符优先级++i

int i = 0
while ( i++ <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }

    // Presumably there would be some code here, or this doesn't really make much sense
}

I would question the use of a whileloop for this kind of structure though. If you want to use a counter in a loop, a forloop is typically more appropriate.

不过,我会质疑while对这种结构使用循环。如果您想在循环中使用计数器,for循环通常更合适。

回答by Aniket Inge

instead of a quickfix solution, lets see your code for a minute and step through it line by line:

而不是快速修复解决方案,让我们看看你的代码一分钟,然后一行一行地逐步完成:

int i = 0;
while ( i <= 10 )
{
    System.out.println(i);
    if ( i == 8 )
    {
        continue;
    }
    i++;
}

iat first is 0, its less than 10 and hence it enters the loop, prints 0 and increments to 1. Then ibecomes 2, 3, 4, .. 8

i起初是 0,它小于 10,因此它进入循环,打印 0 并增加到 1。然后i变成 2, 3, 4, .. 8

When it becomes equal to 8, instead of incrementing, it pops back to the beginning of the loop, printing 8 again.. checks for the value of i(which is 8) and continues again, printing 8.. and it will keep doing this until eternity.

当它变得等于 8 时,它不是递增,而是弹回到循环的开头,再次打印 8.. 检查的值i(即 8)并再次继续,打印 8.. 它将继续这样做直到永恒。

Hence, increment the number before testing and it will work as expected.

因此,在测试之前增加数字,它将按预期工作。

Change your code to something like this

把你的代码改成这样

int i = 0;
while ( i <= 10 )
{
    if ( i != 8 )
    {
        System.out.println(i);
    }
    i++;
}

回答by AlvinfromDiaspar

I like Eric Petroelje's answer. I suggest doing something like this:

我喜欢 Eric Petroelje 的回答。我建议做这样的事情:

if (++i >= 8) continue;

In addition, these days compilers are good enough to warn you about this as a possible infinite loop. There are also code analysis tools that will also detect this for you.

此外,现在编译器足以警告您这是一个可能的无限循环。还有一些代码分析工具也可以为您检测到这一点。

回答by Erik

While this is not code I would ever recommend usingin most cases, it does serve the purpose:

虽然这不是我在大多数情况下推荐使用的代码,但它确实达到了以下目的:

int i = 0;
while ( i <= 10 )
{
  Console.WriteLine(i.ToString());
  if ( i == 8 )
  {
    // Do some work here, then bail on this iteration.
    goto Continue;
  }

Continue:  // Yes, C# does support labels, using sparingly!
  i++;
}