继续,并与一个扩展范围破 [英] continue and break with an extended scope
本文介绍了继续,并与一个扩展范围破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能为继续
或破发
有比当前正在运行的循环更大的范围有多大?< BR>
在下面的例子中,我渴望继续在外部for循环时 EXPR
是真实的,虽然它是所谓的内部进行循环,因此,无论是 [某种内在code]
和 [一些外code]
被执行。
Is there a possibility for a continue
or break
to have a bigger scope than the currently running loop?
In the following example, I desire to to continue on the outer for-loop when expr
is true, although it is called in the inner for-loop, so that neither [some inner code]
nor [some outer code]
is executed.
for(int outerCounter=0;outerCounter<20;outerCounter++){
for(int innerCounter=0;innerCounter<20;innerCounter++){
if(expr){
[continue outer]; // here I wish to continue on the outer loop
}
[some inner code]
}
[some outer code]
}
在上述
推荐答案
您可以使用 GOTO
如果你绝对必须。不过,我通常采取两种方法之一:
You can use goto
if you absolutely must. However, I typically take one of two approaches:
- 请内环一个单独的方法返回
布尔
,这样我可以从它返回,并指出哪些外环应该做的(休息,继续,或做休息循环体) - 在保持独立的
布尔
标志,它是内循环之前设置,可能会被修改的在的内循环,然后被选中的在的内环
- Make the inner loop a separate method returning
bool
, so I can just return from it and indicate what the outer loop should do (break, continue, or do the rest of the loop body) - Keep a separate
bool
flag which is set before the inner loop, may be modified within the inner loop, and then is checked after the inner loop
这些方法我的一般的preFER的提取方法的方法。
Of these approaches I generally prefer the "extract method" approach.
这篇关于继续,并与一个扩展范围破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文