我怎样才能继续上循环

发布于 2025-01-02 14:01:41 字数 1382 浏览 0 评论 0原文

我有这样的代码:

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 。我该怎么做?

I have this code:

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++;
                //}
            }
        }

but I want to have continue for foreach in while loop. how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2025-01-09 14:01:41

您无法从内部循环继续外部循环。
您有两个选择:

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

  2. 好处:只需将您的大意大利面条代码重构为一组函数,这样您就没有内部循环。

You cannot continue an outer loop from an inner one.
You have two options:

  1. The bad one: set a boolean flag before breaking the inner loop, then check this flag and continue if it is set.

  2. The good one: simply refactor your big spagetti code into a set of functions so you do not have inner loops.

维持三分热 2025-01-09 14:01:41

您可以break 跳出while 循环,并继续执行外部foreach 循环的下一次迭代,这将启动一个新的while 循环:

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

如果在 while 循环之后有一些您不希望在这种情况下执行的其他代码,您可以使用一个布尔变量,该变量将在跳出循环之前设置while循环使这段代码不执行并自动跳转在 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
}

You could break out of the while loop and move on to the next iteration of the outer foreach loop which will start a new while loop:

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

If you had some other code after the while loop that you don't want to be executed in this case you could use a boolean variable which will be set before breaking out of the while loop so that this code doesn't execute and automatically jump on the next iteration of the forach loop:

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
}
各自安好 2025-01-09 14:01:41

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

你可以这样做:

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

如果其他人必须处理代码,请小心,确保他们没有 goto-phobic。

In my opinion, using goto is justifiable within complex nested loops (whether or not you should avoid using complex nested loops is a different question).

You could do this:

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

Just be careful if other people have to deal with the code, make sure they're not goto-phobic.

凹づ凸ル 2025-01-09 14:01:41

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

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

希望这对您有帮助。

you could try to use predicates for the inner for loop like:

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

Hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文