5.2 Sequential Structure
- The basic structure of Java is the "sequential structure". Now the basic structure of many languages is the sequential structure.
- Sequential structure is also the simplest algorithm structure
- Statements and statements, boxes and boxes are in top-down order, it is composed of several sequential processing steps, it is a basic algorithm structure that can not be separated from any algorithm!
5.3 Selection Structure
- If single-selection structure (if true)
- If double-selection structure (if)
- If multiple-selection structure (if nested sequentially)
- If nested structure (if nested if)
- switch multi-selection structure (rarely used)
5.3.1 if single-selection structure
A single-choice structure, also known personally as the "if true" statement.
Meaning: Only one if statement, if the condition is true, will execute the code statement inside if! So I call it a "if true" statement.
package structure; import java.util.Scanner; public class If true { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter:"); String str = scanner.nextLine(); //equals to determine if strings are equal if(str.equals("Hello")) { System.out.println("String is really Hello"); } scanner.close(); } }
You will find that it only gives you valid information when the condition is true! It doesn't give you valid information when the condition is false. So at this point, we need a double-selection structure.
5.3.2 Double Selection Structure
The double-selection structure is also known as the "if" statement.
Meaning: I personally think that if a logical judgment statement is "if", it means judgment and feedback in both cases of conditional establishment and conditional failure. This can be called if.
package structure; import java.util.Scanner; public class If { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); if(str.equals("Hello")) { System.out.println("Conditions are true"); } else{ System.out.println("Conditions not established"); } } }
5.3.3 Multiple Selection Structure
The multiple-choice structure is also known to me as the sequential nesting of if.
Meaning: Because of its grammatical format, it is similar to nesting, but not as effective. It looks like a lot of judgments are grouped together, trying to figure out some conditions.
import java.util.Scanner; public class if The sequential nesting of { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your results:"); int num = scanner.nextInt(); if(num >= 60 && num <70) { System.out.println("Congratulations, better pass!"); } else if(num < 60) { System.out.println("Sorry, you scored less than 60 points and did not pass the grade line!"); } else if(num >= 70 && num <80) { System.out.println("Performance Rating: B"); } else if(num >= 80 && num <85) { System.out.println("Performance Rating: B+"); } else if(num > 85 && num <=90) { System.out.println("Performance Rating: A"); } else { System.out.println("Performance Rating: A+"); } } }
5.3.4 if internal nesting
if's internal nesting
Meaning: if can be nested inside each structure or within its own if statement.
package structure; import java.util.Scanner; public class if Internal Nesting { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); if(str.equals("start")) { System.out.println("Please enter your results:"); int num = scanner.nextInt(); if(num >= 60 && num <70) { System.out.println("Congratulations, better pass!"); } else if(num < 60) { System.out.println("Sorry, you scored less than 60 points and did not pass the grade line!"); } else if(num >= 70 && num <80) { System.out.println("Performance Rating: B"); } else if(num >= 80 && num <85) { System.out.println("Performance Rating: B+"); } else if(num > 85 && num <=90) { System.out.println("Performance Rating: A"); } else { System.out.println("Performance Rating: A+"); } } else if(str.equals("break")) { System.out.println("return Return to finish the program!"); return; } else{ System.out.println("Please enter start or break!"); } } }