Continue和Break

break的用法:

1.break用于switch语句中,终止switch语句
2.break用于循环时,跳出循环
3.break用于其他位置,毫无意义

1. break用于switch语句中,终止switch语句

public class for0915{
    public static void main(String[] args){
        int a=2;
        switch (a) {
            case 1:
                System.out.println("天");
            case 2:
                System.out.println("地");
            case 3:
                System.out.println("玄");
                break;
            case 4:
                System.out.println("黄");
            default:
                System.out.println("输入有误");
        }
    }
}

 

continue语句用于终止本次循环,接着开始下一次循环。

如:public class for0915 {
    public static void main(String[] args){
        for(int i =0;i<5;i++){
            System.out.println("i的值是:"+i);
            if(i==3){
                //跳过此次循环
                continue;
            }
            System.out.println("continue后的输出语句:");
        }
    }
}

上一篇:python学习日记(3)


下一篇:jsva-day19_5