Understanding of switch...case

class CodeRunner{
    
    
	public static void main(String[] args){
    
    
		switch(option) {
    
    
    		 case 1: System.out.println("option 1");
   			 case 2: System.out.println("option 2");break;
    		 case 3: System.out.println("option 3"); 
    		 default: System.out.println("我的博客");         
}
	}
}

Question: Whenoption=1,2,3,4What are the results when?
Take the first one as an example:

class CodeRunner{
    
    
	public static void main(String[] args){
    
    
		var option=1;
		switch(option) {
    
    
    		 case 1: System.out.println("option 1");
   			 case 2: System.out.println("option 2");break;
    		 case 3: System.out.println("option 3"); 
    		 default: System.out.println("我的博客");         
}
	}
}

Result value:

option 1
option 2 

By analogy, all the answers are:

var option=1;
option 1
option 2 

var option=2;
option 2

var option=3;
option 3
我的博客

var option=4;
我的博客

When there is no break in a case sentence, it will execute until it encounters a break! . Type the code yourself, you will remember it longer

Guess you like

Origin blog.csdn.net/weixin_43814775/article/details/105401824