【问题标题】:How can I continue The upper Loop我怎样才能继续上循环
【发布时间】:2012-02-27 05:32:48
【问题描述】:

我有这个代码:

foreach(int i in Directions)
        {
            if (IsDowner(i))
            {
                while (IsDowner(i))
                {
                    continue;
                    //if (i >= Directions.Count)
                    //{
                    //    break;
                    //}
                }

                //if (i >= Directions.Count)
                //{
                //    break;
                //}

                if (IsForward(i))
                {

                        continue;
                        //if (i >= Directions.Count)
                        //{
                        //    break;
                        //}

                    //check = true;
                }

                //if (i >= Directions.Count)
                //{
                //    break;
                //}

                if (IsUpper(i))
                {
                        //if (i >= Directions.Count)
                        //{
                        //    break;
                        //}
                    num++;
                    //check = false;
                }

                //if (check)
                //{
                //    num++;
                //}
            }
        }

但我想在while 循环中为foreach 使用continue。我该怎么做?

【问题讨论】:

标签: c# c#-4.0 loops foreach while-loop


【解决方案1】:

您可以将break 退出while 循环并继续进行外部foreach 循环的下一次迭代,这将开始一个新的while 循环:

foreach(int i in Directions)
{
    while (IsDowner(i))
    {
        break;
    }
}

如果您在 while 循环之后有一些其他代码,您不想在这种情况下执行,您可以使用一个布尔变量,该变量将在跳出 @9​​87654327@ 循环之前设置,以便此代码不会执行并自动跳转到 forach 循环的下一次迭代:

foreach(int i in Directions)
{
    bool broken = false;
    while (IsDowner(i))
    {
        // if some condition =>
        broken = true;
        break;
    }

    if (broken) 
    {
        // we have broken out of the inner while loop
        // and we don't want to execute the code afterwards
        // so we are continuing on the next iteration of the
        // outer foreach loop
        continue;
    }

    // execute some other code
}

【讨论】:

  • 但我在 while 循环之后还有另一个代码,我想拥有它们以及下一个 foreach 变量(i)。
  • @ahmadalishafiee,你可以使用布尔变量。
  • 问题已编辑。但是按照你的方式,我的代码中有很多嵌套的 if
  • 你可以使用if(!broken) {...}而不是if(broken) continue;
  • @ahmadalishafiee,你不能 continue 来自内循环的外循环。您首先需要跳出内部循环。此外,正如 Piotr Auguscik 建议的那样,您可以反转条件。我已经用一个例子更新了我的答案。
【解决方案2】:

您不能从内部循环继续外部循环。 你有两个选择:

  1. 不好的:在中断内部循环之前设置一个布尔标志,然后检查这个标志,如果设置了就继续。

  2. 好方法:只需将大型 spagetti 代码重构为一组函数,这样就没有内部循环。

【讨论】:

    【解决方案3】:

    在我看来,在复杂的嵌套循环中使用 goto 是合理的(是否应该避免使用复杂的嵌套循环是另一个问题)。

    你可以这样做:

    foreach(int i in Directions)
    {
        while (IsDowner(i))
        {
            goto continueMainLoop;
        }
        //There be code here
    continueMainLoop:
    }
    

    如果其他人必须处理代码,请小心,确保他们没有恐惧症。

    【讨论】:

      【解决方案4】:

      您可以尝试对内部 for 循环使用谓词,例如:

          foreach (item it in firstList)
           {
              if (2ndList.Exists(x => it.Name.StartsWith(x))) //use predicate instead of for loop.
                  {
                      continue;
                  } 
           }
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-09-18
        • 2013-08-18
        • 2020-07-22
        • 2019-07-13
        • 2014-05-14
        • 2020-08-14
        • 2020-08-24
        • 1970-01-01
        • 2022-12-18
        相关资源
        最近更新 更多