3

这是我的代码架构:

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

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

谢谢!

4

6 回答 6

5

这里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
}
于 2011-01-28T09:23:23.760 回答
2
while (..)
{
   for (...; ...;...)
        for(...;...;...)
            if ( )
            {
                 ...
                 goto superpoint;
            }
superpoint:
//dosomething
} 
于 2011-01-28T09:23:58.663 回答
2

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

while (..)
{
    bool goToWhile = false; 

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

不过想出更好的名字;)

于 2011-01-28T09:25:53.373 回答
1

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

于 2011-01-28T09:23:11.483 回答
1

continueorbreak总是用于接受 a continueor的最内层循环break。在这种情况下,它是代码中最低的for循环。

于 2011-01-28T09:23:25.150 回答
0

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

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

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

希望这可以帮助!

于 2011-01-28T09:22:24.493 回答