【发布时间】:2015-05-27 16:38:00
【问题描述】:
我可以在嵌套的 for 循环中使用 break 以返回到外部 while 循环并在 for 循环内部使用 continue 来强制 while 循环继续运行吗?我无法将 for 循环条件放入我的 while 循环条件中,因此如果我无法在特定满足的情况下继续,while 循环可能会停止。
while(...some conditions...){
...print stuff
for(...some instances are arrays, condition loop array...){
if(...index meets conditions...){
...print once, some arrays might meet condition on multiple index
break; //to prevent multiple printings
}
continue; //i don't want to force another while iteration if(false)
//or is this continue for(loop) anyway?
}
continue; //is this not essentially a while(true) loop with no return?
}
我无法将 for 循环条件放入 while 条件的原因是因为两个循环之间有更多 if 条件,例如 if(array == null) 和 if-condition x == true getArray() 如果数组是则需要调用没有传入。大部分时间条件y 和z 从while 循环打印,但有时条件x 满足,所以我需要for 循环。在 for-loop if(index true)) 的打印之后,我需要 while-loop 再次运行,我被困住了吗?有时这可能会发生在 while-loop 条件下,但我可以看到它不会总是如此,如果 for-loop if(index false)) 满足,我不想强制 while 循环,因为这可能会在运行时处理和可能会导致无限循环。
PS 我是一名初级程序员,我什至不确定这可能吗? 或有道理,如果这是一个愚蠢的问题,对不起
【问题讨论】:
-
您可以使用标签来中断或继续循环stackoverflow.com/questions/886955/…
-
我是一位经验丰富的程序员,使用 break 和 continue,但会保持简单。拥有
boolean active = ...; while (active) {可以提供更多控制权。 -
@usb 我同意,但没有任何其他信息,如果某个条件为真,这是跳出循环的最干净的方法之一。