如何打破在while循环中继续嵌套for循环?

Jos*_*ere 2 java arrays loops break

我可以在嵌套的for循环中使用break来返回外部while循环并continue从for循环中使用以强制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?
}
Run Code Online (Sandbox Code Playgroud)

我无法将for循环条件纳入while条件的原因是因为如果没有传入数组,则需要调用两个循环之间的条件if(array == null)和if条件.x == true getArray()如果大多数情况下条件yz打印来自while-loop但有时x满足条件所以我需要for循环.这是在for-loop打印之后,if(index true))我需要再次循环才能再次使用?有时这可能发生在while循环条件下,但我可以看到它不会总是,如果for循环if(index false))满足则更多我不想强制while循环,因为这可能会在运行时处理中变得昂贵并且可能导致在无尽的循环中.

PS我是一名初级程序员,我甚至不确定这是可能的吗?或者说有道理,抱歉,如果这是一个愚蠢的问题

Som*_*Guy 9

你可以这样命名你的循环:

namedLoop: for(...) {
    // access your namedloop
    break namedLoop;
}
Run Code Online (Sandbox Code Playgroud)

  • 函数应该替换所有label-goto案例.意大利面条代码是维护的噩梦. (3认同)