The difference between multiple ifs and multiple else ifs in Java

  1. Multiple ifs are executed regardless of whether you are in front of ture or not  
  2. else if if one of the former is true, then none of the latter are executed
public class Test {
	public static void main(String[] args) {
		int a = 1;
		if (a == 1) {
			System.out.println("1");
		}
		if (a == 2) {
			System.out.println("2");
		}
		if (a == 3) {
			System.out.println("3");
		}
		if (a == 1) {
			System.out.println("1");
		} else if (a == 2) {
			System.out.println("2");
		} else if (a == 3) {
			System.out.println("3");
		} else if (a == 4) {
			System.out.println("4");
		}
	}

}

 

The level is limited, if you have any questions, please leave a message to exchange.

Learn from each other and make progress together :) Please indicate the source for reprinting. Thank you.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325861957&siteId=291194637