Sequential structure
-
The basic structure of Java is sequential structure. Unless otherwise specified, it will be executed sentence by sentence in order.
-
Sequential structure is the simplest 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 struct; public class ShunXuDemo { public static void main(String[] args) { System.out.println("Hello1"); System.out.println("Hello2"); System.out.println("Hello3"); System.out.println("Hello4"); System.out.println("Hello5"); } }
Select structure
if single selection structure
Many times, it is necessary to judge whether a thing is feasible and then execute it. Such a process is represented by an if statement in the program
Syntax:
If (Boolean expression){
// The statement that will be executed if the Boolean expression is true
}
package struct; import java.sql.SQLOutput; import java.util.Scanner; public class IfDemo01 { 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")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
if double selection structure
There are two judgments and a double selection structure, so there is an 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
}
package 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, and 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,"); } } }
if multiple selection structure
Sometimes there are interval multi-level judgments, and a multi-choice structure is needed to deal with this kind of problem.
Syntax:
If (Boolean expression 1){
// If the value of Boolean expression 1 is true, execute the code
}Else if (Boolean expression 2){
// If the value of Boolean expression 2 is true, execute the code
}Else if (Boolean expression 3){
// 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
}
package struct; import java.util.Scanner; public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the test result:"); int score = scanner.nextInt(); if (score==100){ System.out.println("Congratulations, full marks"); }else if (score<100 && score>=90){ System.out.println("A level"); }else if (score<90 && score>=80){ System.out.println("B level"); }else if (score<80 && score>=70){ System.out.println("C level"); }else if (score<70 && score>=60){ System.out.println("D level");} else if (score<60){ System.out.println("fail,"); }else { System.out.println("The result is illegal!!"); } scanner.close(); } }
An IF statement can have at most one else statement, which follows all else if statements.
An IF statement can have several else if statements, which must precede the else statement.
Once one else if statement is detected as true, the 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. else if...else can be nested like an IF statement.
Syntax:
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
}
}
switch multiple selection structure
Another implementation of the multiple selection structure is the switch case statement.
The switch case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch.
The variable types in the switch statement can be:
byte, short, int, or char.
Starting with Java SE 7, switch supports String type.
At the same time, the case tag must be a string constant or literal.
package struct; public class SwitchDemo01 { public static void main(String[] args) { //case penetration / / switch matches a specific value char grade = 'C'; switch (grade){ case 'A': System.out.println("excellent"); break; //Optional case 'B': System.out.println("good"); break; //Optional case 'C': System.out.println("pass"); break; //Optional case 'D': System.out.println("make persistent efforts"); break; //Optional case 'E': System.out.println("fail"); break; //Optional default: System.out.println("Unknown level"); } } }
case has penetration ability. You can exit the switch structure only when you meet the break statement, otherwise it will be executed continuously.
switch supports String type instances: (using jdk8/11)
package struct; public class SwitchDemo02 { public static void main(String[] args) { String city = "Nanjing"; //JDK7 new features, the expression result can be a string!!! //String or number //Decompile Java -- class (bytecode file) -- decompile (IDEA) switch (city){ case "Beijing": System.out.println("1"); break; case "Nanjing": System.out.println("2"); break; case "Shanghai": System.out.println("3"); break; } } }