2-4、流程控制语句

语雀原文链接

1、顺序结构

  • 程序默认从上往下依次执行

2、分支结构

2-1、if else分支语句

        int a = 90;
        int b = 70;

        if (a > b) {
            System.out.println("a>b");
        }

        if (a > b) {
            System.out.println("a>b");
        } else {
            System.out.println("a<=b");
        }

        if (a > 90) {
            System.out.println("优秀");
        } else if (a > 60) {
            System.out.println("良好");
        } else {
            System.out.println("不及格");
        }

2-2、switch分支语句

  • case后的值不能重复
  • 都不满足,default要执行的
  • 如果满足其中一个case,后续的case就不会判断,直接执行语句,直到遇到break后才不会执行后续的case;
  • switch能够作用的数据类型:byte short int char String
  • jdk1.7及其以后的版本,switch可以作用在String上
  • jdk1.6及其以前的版本,switch不可以作用在String上
        int week = 4;

        switch (week) {
            case 1:
                System.out.println("今天是星期1");
                break;
            case 2:
                System.out.println("今天是星期2");
                break;
            case 3:{
                System.out.println("今天是星期3");
                break;
            }
            case 4: {
                System.out.println("今天是星期4");
//                break;
            }
            case 5:
                System.out.println("今天是星期5");
//                break;
            case 6:
                System.out.println("今天是星期6");
                break;
            case 7:
                System.out.println("今天是星期7");
                break;
            default:
                System.out.println("错误日期");
                break;
        }
  • switch判断不能为null
        // switch不能有null, 否则java.lang.NullPointerException 
        String str = null;
        switch (str){
    
    
            case "1":
                break;
            case "2":
                break;
        }

3、循环语句

3-1、for循环

        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
        
        
    		//死循环
				for(;;){
    			System.out.println("aa");
				}    

3-2、while循环

  • 先判断,后执行
  • 可能一次都没有执行
        int a = 1;
        while (a<100) {
            System.out.println(a);
            a++;
        }

3-3、do…while循环

  • 先执行,后判断
  • 至少执行一次
        int a = 1;

        do{
            System.out.println(a);
            a++;
        }while (a<100);

3-4、break

  • 退出当前循环
class Solution {

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        System.out.println("test start");
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            if (i == 5){
                break;
            }
        }
        System.out.println("test end");
    }
}

上述代码运行结果:
test start
0
1
2
3
4
5
test end
  • break自定义标签跳出循环,与上述代码功能一样,但是这种带标签的写法基本不用
        aaaa:
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            if (i == 5){
                break aaaa;
            }
        }

        System.out.println("-----------");
  • return也可以实现退出循环,但是reture退出的是整个函数
class Solution {
    
    public static void main(String[] args) {
        test();
    }

    private static void test() {
        System.out.println("test start");
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            if (i == 5){
                return;
            }
        }
        System.out.println("test end");
    }
}

上述代码运行结果

test start
0
1
2
3
4
5

3-5、continue

  • continue:结束本次循环,进行下一次循环
        // 不打印5
        for (int i = 0; i < 10; i++) {
            if (i == 5){
                continue;
            }
            System.out.println(i);
        }

最终输出结果:
0
1
2
3
4
6
7
8
9

猜你喜欢

转载自blog.csdn.net/sinat_35615296/article/details/134925165