继续,并与一个扩展范围破并与、范围、继续

2023-09-03 04:56:06 作者:゛曾经的现在、视乎离ヽ

是否有可能为继续破发有比当前正在运行的循环更大的范围有多大?< 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.