继续;用于跳过许多循环

发布于 2024-10-14 21:11:09 字数 268 浏览 6 评论 0原文

这是我的代码的架构:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 continue;
            }
} 

继续会做什么?他只会让第二个循环迭代一次,不是吗? 我希望它能够到达 while,这可能吗?

谢谢!

Here is a schema of my code :

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 continue;
            }
} 

What will do the continue? He will only make the second loop iterate one time, no?
I would like it to reach the while, is it possible ?

Thanks!

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

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

发布评论

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

评论(6

千笙结 2024-10-21 21:11:09

这里的 continue 会影响最近的循环 - 您的第二个 for。有两种方法可以直接跳转到 while

  • goto,尽管有时“被认为是有害的”,但这可以说是它仍然存在的主要原因 >
  • return

来说明后者:

while (..)
{
    DoSomething(..);
}

void DoSomething(..) {
    for (...; ...;...)
      for(...;...;...)
          if ( )
          {
             ...
             return;
          }
}

和前者:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto continueWhile;
            }
   continueWhile:
       { } // needs to be something after a label
}

The continue here impacts the nearest loop - your second for. There are two ways of jumping directly to the while:

  • goto, although sometimes "considered harmful", this is arguably the main reason why it still exists
  • return

To illustrate the latter:

while (..)
{
    DoSomething(..);
}

void DoSomething(..) {
    for (...; ...;...)
      for(...;...;...)
          if ( )
          {
             ...
             return;
          }
}

and the former:

while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto continueWhile;
            }
   continueWhile:
       { } // needs to be something after a label
}
谎言 2024-10-21 21:11:09
while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto superpoint;
            }
superpoint:
//dosomething
} 
while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto superpoint;
            }
superpoint:
//dosomething
} 
错爱 2024-10-21 21:11:09

您应该设置一个变量来确定何时需要离开循环。

while (..)
{
    bool goToWhile = false; 

    for (...; ... && !goToWhile; ...)
        for (...; ... && !goToWhile; ...)
            if ( )
            {
                ...
                goToWhile = true; 
            }
} 

不过想出更好的名字;)

You should set a variable to determine when you need to leave the loops.

while (..)
{
    bool goToWhile = false; 

    for (...; ... && !goToWhile; ...)
        for (...; ... && !goToWhile; ...)
            if ( )
            {
                ...
                goToWhile = true; 
            }
} 

Come up with better names though ;)

童话 2024-10-21 21:11:09

不可能直接作为 continue; 只继续当前循环的执行,要进入外循环,您唯一能做的就是设置一些标志并在外循环中检查它

Not possible directly as continue; only continues the execution of the current loop, to go to outer loop the only thing you can do is set some flags and check it in outer loop

一身骄傲 2024-10-21 21:11:09

continuebreak 始终适用于接受 continuebreak 的最内层循环。在本例中,它是代码中最低的 for 循环。

continue or break is always for the most inner loop which accepts a continue or break. In this case, it's the lowest for loop in your code.

榆西 2024-10-21 21:11:09

仅使用 continue 语句是不可能的。 continue 和break 语句仅影响它们嵌套的最内层循环。

您可以设置一个变量并在外循环中检查它。或者重新组织一下for语句中的IF语句和break条件。

while (..)
{
   for (...; ...;...)
   {
        for(...;...;...)
            if ( )
            {
                 ...
                 skipLoop = true
            }
         if (skipLoop)
             continue;
    }
} 

希望这有帮助!

It's not possible only with continue statement. Continue and break statement affect only the the most inner loop that their are nested.

You can set a variable and check it in the outer loop. Or reorganize the IF statement and the break condition in the for statement.

while (..)
{
   for (...; ...;...)
   {
        for(...;...;...)
            if ( )
            {
                 ...
                 skipLoop = true
            }
         if (skipLoop)
             continue;
    }
} 

Hope this helps!

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