Author homepage : Designer Xiaozheng
Author brief introduction : A Java full-stack software engineer from Ningbo, Zhejiang, responsible for the development and management of the company's OA projects, focusing on front-end and back-end software development (Vue, SpringBoot and WeChat applets), system customization, and remote technical guidance. Certified lecturer of CSDN Academy and Blue Bridge Cloud Class, high-quality creator in the full-stack field. Love technology, focus on business, open cooperation, willing to share, look forward to growing up with you and me!
Main direction : Vue, SpringBoot, WeChat applet
The if statement is a type of Java selection structure, and it is also the most commonly used type. This article will explain the if statement in Java.
Table of contents
1. Why use the if statement
In the previous article, we talked about the most basic sequential structure. The sequential structure is the simplest and most basic flow control in the program. It is executed in sequence according to the sequence of the code.
But sometimes code blocks need to be executed according to specified conditions , such as:
If it's going to rain today, you need an umbrella.
If I have no money, I need to make money.
…
The same is true in Java, it just means if 语句
that this 如果
condition can be expressed more easily.
1. Basic if statement
The basic syntax is as follows:
if (关系表达式) {
语句体;
}
A sample is as follows:
import java.util.Objects;
public class Main {
public static void main(String[] args) {
/**
* 今天如果要下雨,就需要带伞
*/
String weather = "下雨";
if(Objects.equals("下雨",weather)) {
System.out.println("需要带伞");
}
}
}
Exam/Interview Notes
If there are multiple conditions in the relational expression of the if statement, when linked with AND
or , if the former condition makes the expression false, the latter expression will not be executed !OR
If the statement body of the if statement has only one line of code, the braces can be omitted. But from a normative point of view, it is recommended to add it.
2. The if-else statement
The basic syntax is as follows:
if (关系表达式) {
语句体1;
} else {
语句体2;
}
A sample is as follows:
import java.util.Objects;
public class Main {
public static void main(String[] args) {
/**
* 今天如果要下雨,就需要带伞
*/
String weather = "下雨";
if(Objects.equals("下雨",weather)) {
System.out.println("需要带伞");
} else {
System.out.println("不用带伞");
}
}
}
First calculate the value of the relational expression, if it is true execute statement body 1, otherwise execute statement body 2 .
Three, if - else if - else statement
The basic syntax is as follows:
//格式:
if (关系表达式 1) {
语句体1;
} else if (关系表达式 2){
语句体2;
}
...
else {
语句体n+1;
}
A sample is as follows:
import java.util.Objects;
public class Main {
public static void main(String[] args) {
/**
* 今天如果要下雨,就需要带伞
*/
String weather = "下雨";
if(Objects.equals("下雨",weather)) {
System.out.println("需要带伞");
} else if(Objects.equals("阴天",weather)){
System.out.println("我再想想");
} else {
System.out.println("不用带伞");
}
}
}
First calculate the value of the relational expression, if it is true, execute the statement body 1, otherwise continue to judge the relational expression 2 , and so on.
Fourth, the "harm" of the if statement
Appropriate if statements can quickly realize conditional judgment.
However, for conditional judgments with high complexity, other syntaxes can be used to implement them.
Too many if - else statements will make the code too bloated, resulting in situations that cannot be read and understood.
We can use enumeration, polymorphism, etc. to optimize, which will be explained in detail in the following lessons.
5. Lesson Summary
In this class, the if statement of Java's selection structure is firstly explained, and then the specific application of if, if - else, if - else if - else statement is demonstrated. In the next class, the syntax of Java's switch statement will be explained.