Java terminates nested for loop summary

public class NestedLoopDemo {
    public static void main(String[] args) {
        // 第一步:构建嵌套for 循环
        for(int i =0; i< 10; i++){
            for(int j =0; j< 10; j++){
                System.out.println("j is:"+ j );
            }
            System.out.println("i is:" + i);
        }

        // 第二步:使用变量停止嵌套for 循环
        boolean target = true;
        for(int i =0; i< 10 && target; i++){
            for(int j =0; j< 10 && target; j++){
                if(i == 5) {
                    target = false;
                }
                System.out.println("j is:"+ j );
            }
            System.out.println("i is:" + i);
        }

        // 第三步:使用break停止嵌套for 循环:仅仅跳过嵌套for循环的遍历输出,没有达到终止嵌套for 循环
        for(int i =0; i< 10; i++){
            for(int j =0; j< 10; j++){
                if(i == 5) {
                  break;
                }
                System.out.println("j is:"+ j );
            }
            System.out.println("i is:" + i);
        }

        // 第三步:使用continue停止嵌套for 循环:仅仅跳过嵌套for循环的遍历输出,没有达到终止嵌套for 循环
        for(int i =0; i< 10; i++){
            for(int j =0; j< 10; j++){
                if(i == 5) {
                    continue;
                }
                System.out.println("j is:"+ j );
            }
            System.out.println("i is:" + i);
        }

    }
}

Guess you like

Origin blog.csdn.net/zhouzhiwengang/article/details/130637596