C++:中断主循环

发布于 2024-09-14 02:11:24 字数 421 浏览 13 评论 0原文

我正在准备一些代码:

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          break;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}

我想通过在 b 循环中使用 break; 语句(循环变量 int a)来中断主循环(循环变量 int a) b)。

我该怎么做呢?

I am preparing some code:

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          break;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}

I want to break the main loop (loop variable int a) by using a break; statement in the b loop (loop variable int b).

How can I do it?

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

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

发布评论

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

评论(11

红墙和绿瓦 2024-09-21 02:11:24

使用goto

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          goto loopDone;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}
loopDone:

Use a goto.

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          goto loopDone;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}
loopDone:
莳間冲淡了誓言ζ 2024-09-21 02:11:24

要么执行以下四件事之一:使用 goto、使用 throw、使用标志或重构。

许多人不同意使用goto,但有时这是一个干净的解决方案。 (大多数时候,它不是,但它的存在是有原因的。)但是,我发现使用 goto 需要重构。

第二种解决方案是抛出一些特殊异常,然后捕获它在主循环之外。这是对异常系统的滥用,基本上是一个更糟糕的goto;使用 goto 代替它。

第三种解决方案是使用 某种。这基本上是一个“更安全”的goto,但有些人可能会认为它有点难看。 (尤其是多级别。尽管在这种情况下,您担心的是代码有多难看。)

我推荐的解决方案是重构。无论你在做什么,都太过分了。您应该将内部循环移至函数中,然后调用该函数。返回主循环只是从该函数返回。 (换句话说,“我的工作完成了。”)

Either do one of four things: use goto, use throw, use a flag, or refactor.

Many will disagree with using goto, but sometimes it's a clean solution. (Most times, it isn't, but it exists for a reason.) However, I find the use of goto warrants a refactor.

The second solution is to throw some special exception and then catch it just outside the main loop. This is an abuse of the exception system and basically a worse goto; use a goto instead of this.

The third solution would be to use a flag of some sort. This is basically a "safer" goto, but some might argue it's a bit uglier. (Especially with multiple-levels. Though in such a case your concern is how ugly your code is.)

The solution I would recommend is refactor. Whatever you're doing, it's too much. You should move the inner loops into a function, and call that function. Returning to the main loop is simply a return from that function. (In other words "My work is done.")

誰ツ都不明白 2024-09-21 02:11:24

我建议将您的代码重构为函数。然后,您可以从该函数返回,而不是使用break

void myFunc() 
{
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555) // Logic is just an example,
              return;    // since it will always return
        }

        .
        .
        .
    }
}

这(或者甚至可能是更复杂的代码重构)应该有助于提供干净、优雅的解决方案。或者,如果您只想快速修复,可以使用条件变量:

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    bool cond = false;

    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555){
          cond = true;
          break;
       }
    }

    if (cond) break;

    .
    .
    .
}

其他人建议使用 goto。虽然这是另一个快速解决方案,但我强烈建议不要这样做,特别是如果您在严格的环境中工作,代码将在未来几年内接受同行评审和使用。

我看来goto方法比函数/返回重构更难维护,特别是当其他人对代码进行更改时。另外,您还必须向团队中偶然发现代码的其他人证明 goto 的合理性。

I recommend refactoring your code into a function. Then you can just return from that function instead of using break:

void myFunc() 
{
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555) // Logic is just an example,
              return;    // since it will always return
        }

        .
        .
        .
    }
}

This - or perhaps even a more involved refactoring of your code - should lend itself to a clean, elegant solution. Alternatively, if you just want a quick fix you could use a condition variable:

for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    bool cond = false;

    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555){
          cond = true;
          break;
       }
    }

    if (cond) break;

    .
    .
    .
}

Others have suggested using goto. While that is another quick fix, I strongly recommend against it, especially if you are working in a rigorous environment where the code will be peer reviewed and used for years down the road.

In my opinion the goto approach is a bit harder to maintain than a function/return refactoring, especially later on when someone else makes changes to the code. Plus you will have to justify the goto to anyone else on the team that happens to stumble onto the code.

蓝海 2024-09-21 02:11:24
for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          goto end;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}
end:
for(int a = 1; a <= 100; a++)    //loop a (main loop)
{
    for(int b = 1000; b <= 2000; b++)    //loop b
    {
       if(b == 1555)
          goto end;
    }

    for(int c = 2001; c <= 3000; c++)    //loop c
    {
       .
       .
       .
    }
}
end:
习惯成性 2024-09-21 02:11:24

如果合适的话,您可以创建一个内容为循环的函数,并使用 return。

public void bigLoop()
{
    for(int a = 1; a <= 100; a++)
    {
        for(int b = 1000; b <= 2000; b++)
        {
            if(b == 1555)
                return;
        }

        for(int c = 2001; c <= 3000; c++)
        {
            .
            .
            .
        }
    }
}//bigLoop

If it is appropriate, you could make a function who's contents are the a loop, and use return.

public void bigLoop()
{
    for(int a = 1; a <= 100; a++)
    {
        for(int b = 1000; b <= 2000; b++)
        {
            if(b == 1555)
                return;
        }

        for(int c = 2001; c <= 3000; c++)
        {
            .
            .
            .
        }
    }
}//bigLoop
一身软味 2024-09-21 02:11:24

一次退出两个此类循环的唯一方法是 goto 或 throw 或 return 和 throw /code> 和 return 可能不合适(特别是 throw,如果条件不例外)。或者,您可以设置某种条件(bool breakout;),并在为真时继续中断。

The only way to head out of two such loops at a time is a goto or a throw or a return, and throw and return may not be appropriate (particularly throw, if the condition isn't exceptional). Alternately, you could set some sort of condition (bool breakout;), and keep breaking if it's true.

坦然微笑 2024-09-21 02:11:24

\(◕◡◕)/

[]() {
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555)
              return;
        }

        for(int c = 2001; c <= 3000; c++)    //loop c
        {
           .
           .
           .
        }
    }
}();

\ (◕ ◡ ◕) /

[]() {
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555)
              return;
        }

        for(int c = 2001; c <= 3000; c++)    //loop c
        {
           .
           .
           .
        }
    }
}();
方圜几里 2024-09-21 02:11:24
  1. 使用goto

    for(int a = 1; a <= 100; a++) //循环a(主循环)
    {
        for(int b = 1000; b <= 2000; b++) //循环b
        {
           如果(b==1555)
              继续完成;
        }
        for(int c = 2001; c <= 3000; c++) //循环c
        {
           。
           。
           。
        }
    }
    完毕:
    
  2. 设置每个循环测试的哨兵值:

    bool 哨兵 = true ;
    for(int a = 1; a <= 100 && sentinel ; a++) //循环a(主循环)
    {
        for(int b = 1000; b <= 2000 && 哨兵; b++) //循环b
        {
           如果(b==1555)
              哨兵=假;
        }
        for(int c = 2001; c <= 3000 && sentinel; c++) //循环c
        {
           。
           。
           。
        }
    }
    
  1. Use a goto:

    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555)
              goto done;
        }
        for(int c = 2001; c <= 3000; c++)    //loop c
        {
           .
           .
           .
        }
    }
    done:
    
  2. set a sentinel value tested by each loop:

    bool sentinel = true ;
    for(int a = 1; a <= 100 && sentinel ; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000 && sentinel; b++)    //loop b
        {
           if(b == 1555)
              sentinel = false;
        }
        for(int c = 2001; c <= 3000 && sentinel; c++)    //loop c
        {
           .
           .
           .
        }
    }
    
绮筵 2024-09-21 02:11:24

一种简单的策略是将循环放在单独的函数中,并在选定的点执行返回:

void func()
{
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555)
              return;
        }

        for(int c = 2001; c <= 3000; c++)    //loop c
        {
           .
           .
           .
        }
    }
}

也可以使用返回值或函数的引用参数返回任何类型的结果。

One simple strategy is to put the loop in a separate function and do a return at the selected point:

void func()
{
    for(int a = 1; a <= 100; a++)    //loop a (main loop)
    {
        for(int b = 1000; b <= 2000; b++)    //loop b
        {
           if(b == 1555)
              return;
        }

        for(int c = 2001; c <= 3000; c++)    //loop c
        {
           .
           .
           .
        }
    }
}

Any kind of result may also be returned with a return value, or with a reference parameter for the function.

绻影浮沉 2024-09-21 02:11:24

理想的方法是重构代码,以便不再需要如此复杂的嵌套循环结构。根据代码的其余部分的外观,您的 bc 循环可能会成为单独的函数(如果不是整个 a)环形。

既然它看起来像循环 bc 遍历相邻范围,为什么不将它们组合起来并稍微减少循环嵌套呢?

for (int a = 1; a <= 100; a++)    //loop a (main loop)
{
    int count = 1000;
    while (count <= 3000) // combined loops 'b' and 'c'
    {
        if (count <= 2000)
        {
            // Old loop 'b' code
            if (b == 1555)
                goto fullbreak;
        }
        else
        {
            // Old loop 'c' code
            ...
        }
        count++;
    }
}
fullbreak:

您还可以使用条件变量来代替 goto。如果您想跳出旧的 b 循环,但仍处理旧的 c 循环,只需在旧的 内设置 count = 2001 >b 循环代码。

理想情况下,您至少能够将其重构为更像

for (int a = 1; a <= 100; a++)    //loop a (main loop)
{
    if (process_inner_loop(pass, required, args))
        break;
}

函数 process_inner_loop 包装原始两个循环并返回非零(如果您想跳出封闭循环)的地方。现在,您可以简单地 return 1;,而不是使用 goto 或条件变量。

The ideal way would be to re-factor your code so that you no longer need such a complicated nested-loop structure. Depending on what the rest of your code looks like, your b and c loops may be candidates for becoming individual functions, if not the entire a loop.

Since it looks like loops b and c iterate over adjacent ranges, why not combine them and reduce your loop nesting a bit?

for (int a = 1; a <= 100; a++)    //loop a (main loop)
{
    int count = 1000;
    while (count <= 3000) // combined loops 'b' and 'c'
    {
        if (count <= 2000)
        {
            // Old loop 'b' code
            if (b == 1555)
                goto fullbreak;
        }
        else
        {
            // Old loop 'c' code
            ...
        }
        count++;
    }
}
fullbreak:

You can also use a condition variable instead of the goto. If you want to break out of the old b loop but still process the old c loop, simply set count = 2001 inside the old b loop code.

Ideally, you would at least be able to re-factor this to something more like

for (int a = 1; a <= 100; a++)    //loop a (main loop)
{
    if (process_inner_loop(pass, required, args))
        break;
}

where the function process_inner_loop wraps up your original two loops and returns non-zero if you want to break out of the enclosing loop. Now, instead of using goto or condition variables, you can simply return 1;.

剩一世无双 2024-09-21 02:11:24

使用这种模式

for(int a = 1; a <= 100; a++)
{
    int breakMain = 0;
    for(int b = 1000; b <= 2000; b++)
    {
       if(b == 1555)
       {
           breakMain = 1;
           break;
       }
    }

    if(breakMain)
         break;

    for(int c = 2001; c <= 3000; c++)
    {
       .
       .
       .
    }
}

Use this kind of pattern

for(int a = 1; a <= 100; a++)
{
    int breakMain = 0;
    for(int b = 1000; b <= 2000; b++)
    {
       if(b == 1555)
       {
           breakMain = 1;
           break;
       }
    }

    if(breakMain)
         break;

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