Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!
Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!
✪ introduction to Java white 200 case series directory index
◄ previous article 40.Java string comparison
► next 42.switch case statement
if statement is the most used conditional branch structure. It belongs to selection statement and can also be called conditional statement.
If selection structure is a syntax structure that is processed after judgment according to conditions. By default, the if statement controls the execution of the following statement. However, through statement blocks, if statements can control multiple statements.
The simplest syntax format of the if statement is as follows, which means "if certain conditions are met, some processing will be carried out".
if (conditional expression){
Statement block;
}
Example 1
Instance whose judgment condition is true
package demo.demo41; public class Demo1 { public static void main(String[] args) { System.out.println("The program starts running"); //The judgment condition is true, so it will enter the if branch statement if(true){ System.out.println("if The statement was executed"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
The if statement was executed
The program ends running
Example 2
Instance whose judgment condition is false
package demo.demo41; public class Demo2 { public static void main(String[] args) { System.out.println("The program starts running"); //The judgment condition is false, so it will not enter the if branch statement if(false){ System.out.println("if The statement was executed"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
The program ends running
Example 3
User input judgment instance
package demo.demo41; import java.util.Scanner; public class Demo3 { public static void main(String[] args) { System.out.println("The program starts running"); System.out.println("Please enter a number:"); Scanner input = new Scanner(System.in); int num = input.nextInt(); // Receive keyboard input data // Judge whether the data entered by the user is greater than 10 if (num > 10) { System.out.println("The number entered is greater than 10"); } // Judge whether the data entered by the user is equal to 10 if (num == 10) { System.out.println("The number entered is equal to 10"); } // Judge whether the data entered by the user is less than 10 if (num < 10) { System.out.println("The number entered is less than 10"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
Please enter a number:
5
The number entered is less than 10
The program ends running
Operation result 2:
The program starts running
Please enter a number:
11
The number entered is greater than 10
The program ends running
A single if statement can only be used when the condition is met, and cannot perform any other operation (stop). If combined with else statement can define two operations. At this time, if... Else statement means "if the condition is correct, perform one operation, otherwise perform another operation".
The syntax format of the if... else statement is as follows:
if (expression){
Statement block 1;
} else {
Statement block 2;
}
Example 1
When the judgment condition is true
package demo.demo41; public class Demo4 { public static void main(String[] args) { System.out.println("The program starts running"); //The judgment condition is true, so it will enter the if branch statement if(true){ System.out.println("if The statement was executed"); }else { System.out.println("else The statement was executed"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
The if statement was executed
The program ends running
Example 2
When the judgment condition is false
package demo.demo41; public class Demo5 { public static void main(String[] args) { System.out.println("The program starts running"); //The judgment condition is false, so it will enter the else branch statement if(false){ System.out.println("if The statement was executed"); }else { System.out.println("else The statement was executed"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
else statement was executed
The program ends running
Example 3
Size judgment of two numbers
package demo.demo41; public class Demo6 { public static void main(String[] args) { System.out.println("The program starts running"); int num1 = 10; int num2 = 20; // If num1 equals num2 if (num1 == num2) { System.out.println("num1 be equal to num2"); } // If num1 is greater than num2 if (num1 > num2) { System.out.println("num1 greater than num2"); } else { // Otherwise, num1 is less than num2 System.out.println("num1 less than num2"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
num1 is less than num2
The program ends running
The main function of if statement is to provide a branch for the program. However, sometimes only one more branch in the program is not enough, and sometimes the branch of the program will be very complex, which requires the use of multi branch if... else if statements.
It is usually expressed as "if certain conditions are met, some processing will be carried out, otherwise if another condition is met, another processing will be carried out... If these conditions are not met, the last condition will be executed".
The syntax format of if... else if multi branch statements is as follows:
If (expression 1){
Statement block 1;
}Else if (expression 2){
Statement block 2;
}Else if (expression n){
Statement block n;
} else {
Statement block n+1;
}
Example 1
Judge the size of two numbers
package demo.demo41; public class Demo7 { public static void main(String[] args) { System.out.println("The program starts running"); int num1 = 10; int num2 = 20; if (num1 == num2) { // If num1 equals num2 System.out.println("num1 be equal to num2"); } else if (num1 > num2) { // If num1 is greater than num2 System.out.println("num1 greater than num2"); } else { // Otherwise, it is less than System.out.println("num1 less than num2"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
num1 is less than num2
The program ends running
Example 2
Judge students' grades according to the input conditions
package demo.demo41; import java.util.Scanner; public class Demo8 { public static void main(String[] args) { System.out.println("The program starts running"); System.out.println("Please enter the test result:"); Scanner input = new Scanner(System.in); int score = input.nextInt(); // Receive keyboard input data if (score >= 90) { // Test score > = 90 System.out.println("excellent"); } else if (score >= 80) { // 90 > test score > = 80 System.out.println("good"); } else if (score >= 60) { // 80 > test score > = 60 System.out.println("secondary"); } else { // Test score < 60 System.out.println("difference"); } System.out.println("The program ends running"); } }
Operation conclusion: 1:
The program starts running
Please enter the test result:
100
excellent
The program ends running
Operation result 2:
The program starts running
Please enter the test result:
59
difference
The program ends running
The usage of if statement is very flexible. It can not only be used alone, but also nest another if statement in the if statement. Similarly, if... else statement and if... else if statement can also nest another if structure statement to complete deeper judgment.
The syntax format of nested if is as follows:
If (expression 1){
If (expression 2){
Statement block 1;
} else {
Statement block 2;
}
} else {
If (expression 3){
Statement block 3;
}Else if (expression 4){
Statement block 4;
} else {
If (expression n){
Statement block n;
} else {
Statement block n+1;
}
}
}
example
package demo.demo41; public class Demo9 { public static void main(String[] args) { String today = "weekend"; boolean nvpengyou = true;//girl friend System.out.println("The program starts running"); if("weekend".equals(today)){//It's the weekend if(nvpengyou){//Have a girlfriend System.out.println("Take your girlfriend shopping"); }else{//No girlfriend System.out.println("Beat the king at home"); } }else {//Not a weekend System.out.println("Go to work"); } System.out.println("The program ends running"); } }
Operation results:
The program starts running
Take your girlfriend shopping
The program ends running
This section summarizes the "detailed explanation of if else branch structure". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning Java with brother Xiaoming, [pay attention to a wave] won't get lost.
Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!
Navigation✪ introduction to Java white 200 case series directory index
◄ previous article 40.Java string comparison
► next 42.switch case statement
1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory