Java switch语句

Java Switch语句

语法

switch(expression){    
    case value1:    
       //code to be executed;    
       break;  //optional  
    case value2:    
       //code to be executed;    
       break;  //optional  
    ......    

    default:     
       // code to be executed if all cases are not matched;    
}

流程执行图

在这里插入图片描述

如果没有break

public class SwitchExample2 {
    public static void main(String[] args) {
        int number = 20;
        switch (number) {
        case 10:
            System.out.println("10");
        case 20:
            System.out.println("20");
        case 30:
            System.out.println("30");
        default:
            System.out.println("Not in 10, 20 or 30");
        }
    }
}

执行结果:

20
30
Not in 10, 20 or 30

猜你喜欢

转载自blog.csdn.net/weixin_43101144/article/details/83896031