Java uses a labeled break statement to break out of multiple loops

You can define a label outside the multiple loops, and then use the break statement with the label in the loop body to jump out of the multiple loops as follows

 code show as below

    @Test
    void BreakOutOfMultipleLoops() {
        goOut:
        //定义一个标签 标签格式为:标识符+":"组成
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 5; j++) {
                //if (j>=3){break goOut;}花括号可以省略
                if (j >= 3)
                    break goOut;
                System.out.println("当前j为:" + j);

            }
            System.out.println("浪里啷格狼,居然可以直接跳过我~");
        }
        System.out.println("我出来啦");
    }

The result of the operation is as follows

当前j为:0
当前j为:1
当前j为:2
我出来啦

Guess you like

Origin blog.csdn.net/lps12345666/article/details/130004856