How do I break out of nested loops in Java?

前端 未结 30 3323
梦毁少年i
梦毁少年i 2020-11-21 11:51

I\'ve got a nested loop construct like this:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do somethin         


        
30条回答
  •  执笔经年
    2020-11-21 12:10

    If you don't like breaks and gotos, you can use a "traditional" for loop instead the for-in, with an extra abort condition:

    int a, b;
    bool abort = false;
    for (a = 0; a < 10 && !abort; a++) {
        for (b = 0; b < 10 && !abort; b++) {
            if (condition) {
                doSomeThing();
                abort = true;
            }
        }
    }
    

提交回复
热议问题