What is a neat way of breaking out of many for loops at once?

后端 未结 14 1523
既然无缘
既然无缘 2020-12-28 19:33

Suppose I need to break out of three or four nested for loops at once at the occurence of some event inside the innermost loop. What is a neat way of doing that?

14条回答
  •  猫巷女王i
    2020-12-28 19:41

    If you're using Java, you can associate labels with each for block and then reference the label after a continue statement. For example:

    outerfor:
    for (int i=0; i<5; i++) {
        innerfor:
        for (int j=0; j<5; j++) {
            if (i == 1 && j == 2) {
                 continue outerfor;
            }
        }
    }

提交回复
热议问题