Sequential structure
- The basic structure of JAVA is sequential structure. Unless otherwise specified, it will be executed sentence by sentence.
- Sequential structure is a simple algorithm structure
- Between statements and between boxes, it is carried out in the order from top to bottom. It is composed of several processing steps executed in turn. It is a basic algorithm structure that any algorithm is inseparable from.
package com.huang.struct; public class Demo01 { //Sequential structure public static void main(String[] args) { System.out.println("helle1"); System.out.println("helle2"); System.out.println("helle3"); System.out.println("helle4"); System.out.println("helle5"); } }
if single selection structure
-
Many times, we need to judge whether something is feasible before we execute it. Such a process is represented by if statements in the program.
-
grammar
If (Boolean expression){}/ / statement to be executed if Boolean expression is true
package com.huang.struct; import java.util.Scanner; public class ifDemo01 { //Select structure public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter:"); String s = scanner.nextLine(); //equals determines whether the strings are equal if (s.equals("Hello")){ //This code means that if this string is equal to Hello System.out.println(s); } System.out.println("End"); scanner.close(); } }
if double selection structure
-
Now there is a demand. The company wants to buy a software. If it succeeds, it will pay 100w yuan. Failed, find someone to develop
-
Such a requirement cannot be determined with one if, so two judgments are needed. Therefore, a double selection structure is needed. So there is the if else structure.
- Syntax:
if (Boolean expression){
//If the Boolean expression has a value of true
}else{
//If the value of the Boolean expression is false}
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG tageenso-1636717945665) (E: \ javase \ picture \ 131313. PNG)]
- Syntax:
package com.huang.struct; import java.util.Scanner; public class ifDemo02 { public static void main(String[] args) { //If the test score is more than 60, you will pass. If it is less than 60, you will fail Scanner scanner = new Scanner(System.in); System.out.println("Please enter your grade:"); int score = scanner.nextInt(); if (score>60){ System.out.println("pass"); }else { System.out.println("fail,"); } scanner.close(); } }
if multiple selection structure
-
The above dual choice structure is not in line with the actual situation. In the real situation, there may also be ABCD and interval multi-level judgment. For example, 90-100 is A, 80-90 is B... Etc. there are many times in life that there are not only two choices, so A segment choice structure is needed to deal with such problems.
-
grammar
If (Boolean expression 1){
//If the value of Boolean expression 1 is true, execute the code}else if{
//If the value of Boolean expression 2 is true, execute the code
}else if{//If the value of Boolean expression 3 is true, execute the code
}else{//If none of the above Boolean expressions is true, execute the code
}
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-SPECa4PN-1636717945668)(E:\JavaSE\picture1414.png)]
package com.huang.struct; import java.util.Scanner; public class ifDemo03 { //if multiple selection structure public static void main(String[] args) { Scanner scaner = new Scanner(System.in); System.out.println("Please enter a score:"); int score = scaner.nextInt(); if (100 == score ){ System.out.println("Congratulations, full marks"); }else if (score<100 && score>=90){ System.out.println("excellent"); }else if (score <90 && score >=80){ System.out.println("good"); }else if (score<80 && score >=60){ System.out.println("pass"); }else if (score<60 && score >=0){ System.out.println("fail,"); }else { System.out.println("Please input the score correctly!"); } scaner.close(); } } //Summary: the if statement can have at most one else statement. After all else if statements, the if statement can have several else if statements. They must be before the else statement. Once one else if statement is detected as true, other else if and else statements will skip execution
Nested if structure
-
It is legal to use nested if... Else statements, that is, if or else if statements can be used in another if or else if statement, and else if... Else can be nested like an IF statement
-
grammar
If (Boolean expression 1){
//If the value of Boolean expression 1 is true, execute the code
}If (Boolean expression 2){//If the value of Boolean expression 1 is true, execute the code
}
}
Examples
package com.huang.struct; import java.util.Scanner; public class ifDemo04 { //Nested if statement //Input two integers from the keyboard and output their relationship (greater than, less than, equal to) public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the first integer:"); int i = sc.nextInt(); System.out.println("Please enter the second integer:"); int i1 = sc.nextInt(); if (i==i1){ System.out.println(i+"And"+i1+"equal"); }else if (i>i1){ System.out.println(i+"greater than"+i1); }else { System.out.println(i+"less than"+i1); } } }