C# 继续嵌套 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1133408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Continue in nested while loops
提问by SkunkSpinner
In this code sample, is there any way to continue on the outer loop from the catch block?
在此代码示例中,有没有办法从 catch 块继续执行外部循环?
while
{
// outer loop
while
{
// inner loop
try
{
throw;
}
catch
{
// how do I continue on the outer loop from here?
continue;
}
}
}
采纳答案by Eric Lippert
UPDATE: This question was inspiration for my article on this subject.Thanks for the great question!
更新:这个问题启发了我关于这个主题的文章。谢谢你的好问题!
"continue" and "break" are nothing more than a pleasant syntax for a "goto". Apparently by giving them cute names and restricting their usages to particular control structures, they no longer draw the ire of the "all gotos are all bad all the time" crowd.
“continue”和“break”只不过是“goto”的一种令人愉快的语法。显然,通过给它们取可爱的名字并将它们的用法限制在特定的控制结构中,它们不再引起“所有 goto 一直都是坏的”人群的愤怒。
If what you want to do is a continue-to-outer, you couldsimply define a label at the top of the outer loop and then "goto" that label. If you felt that doing so did not impede the comprehensibility of the code, then that might be the most expedient solution.
如果您想做的是继续到外部,您可以简单地在外部循环的顶部定义一个标签,然后“转到”该标签。如果您认为这样做不会妨碍代码的可理解性,那么这可能是最方便的解决方案。
However, I would take this as an opportunity to consider whether your control flow would benefit from some refactoring. Whenever I have conditional "break" and "continue" in nested loops, I consider refactoring.
但是,我会借此机会考虑您的控制流是否会从一些重构中受益。每当我在嵌套循环中有条件“中断”和“继续”时,我都会考虑重构。
Consider:
考虑:
successfulCandidate = null;
foreach(var candidate in candidates)
{
foreach(var criterion in criteria)
{
if (!candidate.Meets(criterion))
{ // TODO: no point in continuing checking criteria.
// TODO: Somehow "continue" outer loop to check next candidate
}
}
successfulCandidate = candidate;
break;
}
if (successfulCandidate != null) // do something
Two refactoring techniques:
两种重构技术:
First, extract the inner loop to a method:
首先,将内部循环提取到一个方法中:
foreach(var candidate in candidates)
{
if (MeetsCriteria(candidate, criteria))
{
successfulCandidate = candidate;
break;
}
}
Second, can allthe loops be eliminated? If you are looping because you are trying to search for something, then refactor it into a query.
第二,能不能把所有的循环都消除掉?如果您因为要搜索某些内容而循环,请将其重构为查询。
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
}
If there are no loops then there is no need to break or continue!
如果没有循环,则无需中断或继续!
回答by Welbog
Swap the try/catch structure with the inner while loop:
用内部 while 循环交换 try/catch 结构:
while {
try {
while {
throw;
}
}
catch {
continue;
}
}
回答by shahkalpesh
No.
I suggest, extracting the inner loop into a separate method.
不,
我建议将内部循环提取到一个单独的方法中。
while
{
// outer loop
try
{
myMethodWithWhileLoopThatThrowsException()
}
catch
{
// how do I continue on the outer loop from here?
continue;
}
}
}
回答by Jake Pearson
You can use a break; statement.
你可以休息一下;陈述。
while
{
while
{
try
{
throw;
}
catch
{
break;
}
}
}
Continue is used to jump back to the top of the current loop.
continue 用于跳回到当前循环的顶部。
If you need to break out more levels than that you will either have to add some kind of 'if' or use the dreaded/not recommended 'goto'.
如果您需要突破更多级别,您将不得不添加某种“if”或使用可怕/不推荐的“goto”。
回答by Marco Mustapic
Use break
in the inner loop.
使用break
在内部循环。
回答by David Basarab
You just want to break from the inner which would continue the outer.
你只想从内部打破,这将继续外部。
while
{
// outer loop
while
{
// inner loop
try
{
throw;
}
catch
{
// how do I continue on the outer loop from here?
break;
}
}
}
回答by Maxim Zaslavsky
I think the best way to accomplish this would be to use the breakstatement. Break ends the current loopand continues execution from where it ends. In this case, it would end the inner loopand jump back into the outer while loop. This is what your code would look like:
我认为实现这一点的最佳方法是使用break语句。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
}
}
}
I believe that is what you were looking to be accomplished, correct? Thanks!
我相信这就是你想要完成的,对吗?谢谢!
回答by zvrba
Use an own exception type, e.g., MyException. Then:
使用自己的异常类型,例如 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)
{ ; }
}
This will work for continuing and breaking out of several levels of nested while statements. Sorry for bad formatting ;)
这将适用于继续和打破嵌套 while 语句的多个级别。抱歉格式不正确;)
回答by ryansstack
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 ;
}
Problem solved. (what?? Why are you all giving me that dirty look?)
问题解决了。(什么??为什么你们都用那种肮脏的眼神看着我?)
回答by ryansstack
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;
}
}
}
}
}