控制跳转语句break和 continue

break

作用:终止switch或者循环

应用场景: 只能在switch和循环中 ,离开使用场景的存在是没有意义的

代码举例:

public class Demo10Break {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            //需求:打印完两次HelloWorld之后结束循环
            if (i == 3) {
                break;
            }
            System.out.println("HelloWorld" + i);
        }
    }
}

执行结果:

continue

作用:结束本次循环,继续下一次的循环

continue的使用场景:只能在循环中
代码举例:

public class Demo11continue {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            //需求:不打印第三次HelloWorld
            if (i == 3) {
                continue;
            }
            System.out.println("HelloWorld" + i);
        }
    }
}

执行结果:

猜你喜欢

转载自www.cnblogs.com/wurengen/p/10847541.html