-->

继续在嵌套while循环(Continue in nested while loops)

2019-06-26 01:19发布

在此代码示例,是否有任何的方式继续对从catch块外环?

while
{
   // outer loop

   while
   {
       // inner loop
       try
       {
           throw;
       }
       catch 
       {
           // how do I continue on the outer loop from here?
           continue;
       }
   }
}

Answer 1:

更新:这个问题问得灵感我对这个问题的文章。 感谢伟大的问题!


“继续”和“突破”只不过是一个愉快的语法的更多的“转到”。 显然,给他们可爱的名字,并限制其用途,以特定的控制结构,他们不再画“所有的goto都是坏所有的时间”人群的愤怒。

如果你想要做的是继续到外,你可以简单地在外环的顶部定义了一个标签,然后选择“转到”这个标签。 如果你觉得这样做并不妨碍代码的可理解性,那么这可能是最有利的解决方案。

不过,我想借此为契机,考虑你的控制流是否会从一些重构中受益。 每当我有有条件的“破”与“继续”在嵌套循环,我认为重构。

考虑:

successfulCandidate = null;
foreach(var candidate in candidates)
{
  foreach(var criterion in criteria)
  {
    if (!candidate.Meets(criterion)) // Edited.
    {  // TODO: no point in continuing checking criteria.
       // TODO: Somehow "continue" outer loop to check next candidate
    }
  }
  successfulCandidate = candidate;
  break;
}
if (successfulCandidate != null) // do something

两个重构技术:

首先,提取内环的方法:

foreach(var candidate in candidates)
{
  if (MeetsCriteria(candidate, criteria))
  { 
      successfulCandidate = candidate;
      break;
  }
}

其次,可以将所有的循环被淘汰? 如果你是循环的,因为你想寻找的东西,然后重构它进入查询。

var results = from candidate in candidates 
              where criteria.All(criterion=>candidate.Meets(criterion))
              select candidate;
var successfulCandidate = results.FirstOrDefault();
if (successfulCandidate != null)
{
  do something with the candidate
}

如果没有环路那么就没有必要打破或继续!



Answer 2:

    while
    {
       // outer loop

       while
       {
           // inner loop
           try
           {
               throw;
           }
           catch 
           {
               // how do I continue on the outer loop from here?
               goto REPEAT;
           }
       }
       // end of outer loop
REPEAT: 
       // some statement or ; 
    }

问题解决了。 (什么??你们为什么都让我一眼?)



Answer 3:

您可以使用中断; 声明。

while
{
   while
   {
       try
       {
           throw;
       }
       catch 
       {
           break;
       }
   }
}

继续用于跳回电流回路的顶部。

如果您需要打破更多的水平比你要么必须添加某种“如果”或使用可怕的/不推荐使用“转到”。



Answer 4:

SWAP与内循环的同时在try / catch结构:

while {
  try {
    while {
      throw;
    }
  }
  catch {
    continue;
  }
}


Answer 5:

没有。
我建议,在提取内环到一个单独的方法。

while
{
   // outer loop
       try
       {
           myMethodWithWhileLoopThatThrowsException()
       }
       catch 
       {
           // how do I continue on the outer loop from here?
           continue;
       }
   }
}


Answer 6:

利用break的内部循环。



Answer 7:

你只是想从内这将继续外突破。

while
{
   // outer loop

   while
   {
       // inner loop
       try
       {
           throw;
       }
       catch 
       {
           // how do I continue on the outer loop from here?
           break;
       }
   }
}


Answer 8:

我想做到这一点是使用break语句的最佳途径。 休息结束当前循环 ,并从它的结束位置继续执行 。 在这种情况下,这将结束该内环跳回到外while循环 。 这是你的代码是什么样子:

while
{
   // outer loop

   while
   {
       // inner loop
       try
       {
           throw;
       }
       catch 
       {
           // break jumps to outer loop, ends inner loop immediately.
           break; //THIS IS THE BREAK
       }
   }
}

我相信这是你要找什么来完成,正确吗? 谢谢!



Answer 9:

using System;

namespace Examples
{

    public class Continue : Exception { }
    public class Break : Exception { }

    public class NestedLoop
    {
        static public void ContinueOnParentLoopLevel()
        {
            while(true)
            try {
               // outer loop

               while(true)
               {
                   // inner loop

                   try
                   {
                       throw new Exception("Bali mu mamata");
                   }
                   catch (Exception)
                   {
                       // how do I continue on the outer loop from here?

                       throw new Continue();
                   }
               }
            } catch (Continue) {
                   continue;
            }
        } 
    }

}

}


Answer 10:

使用自己的异常类型,例如,MyException。 然后:

while
{
   try {
   // outer loop
   while
   {
       // inner loop
       try
       {
           throw;
       }
       catch 
       {
           // how do I continue on the outer loop from here?
           throw MyException;
       }
   }
   } catch(MyException)
   { ; }
}

这将继续,并打破了嵌套while语句的几个层次的工作。 对不起,坏的格式;)



文章来源: Continue in nested while loops