Home  >  Article  >  Java  >  What are the formats of if statements?

What are the formats of if statements?

王林
王林Original
2020-07-01 09:52:1923726browse

The if statement has three formats, namely: 1. [if (Boolean expression) {execution statement}]; 2. [if (Boolean expression) {execution code} else {execution code}]; 3. [if (Boolean expression) {execution code} else if (Boolean expression) {execution code} else {execution code}].

What are the formats of if statements?

if statements have three formats, namely:

(recommended learning: java entry program)

1. If statement

if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}

2. Combined with else statement

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}

3. If statement with else if...else statement

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}
else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}
else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}
else {
   //如果以上布尔表达式都不为true执行代码
}

(Video tutorial recommended : java video tutorial)

Note:

1. Whether the comparison expression is simple or complex, the result must be of Boolean type.

2. If there is only one statement in the statement body controlled by the if statement, the braces can be omitted; if there are multiple statements, they cannot be omitted. It is recommended that they should never be omitted.

For example:

if(比较表达式)
语句

3. Generally speaking: there is no semicolon when there is an opening brace, and there is no opening brace when there is a semicolon.

For example:

if(比较表达式){........},      if(比较表达式);

For example:

 public class HelloWorld {
 
	public static void main(String[] args) {
		int i=10;
		int j=8;
		/*
		 * if语句第一种格式
		 * */
		if(i>j){
			System.out.println("我是if语句第一种格式");
		}
		
		/*
		 * if语句第二种格式
		 * */
		if(i<j){
			System.out.println("我是if语句第二种格式1");
		}else{
			System.out.println("我是if语句第二种格式2");
		}
		
		/*
		 * if语句第三种格式
		 * */
		if(i<j){
			System.out.println("我是if语句第三种格式1");
		}else if(i>j){
			System.out.println("我是if语句第三种格式2");
		}else{
			System.out.println("我是if语句第三种格式3");
		}
	}
}

The output result is as shown in the figure:

What are the formats of if statements?

The above is the detailed content of What are the formats of if statements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn