Java starts from scratch (select the structure), and java learns the structure from scratch
I. program structure:
Generally, the program structure includes the following three types:
1. Sequence Structure 2. Selection structure 3. Cycle Structure 2. Sequence Structure
The program runs row by row. After a statement is executed, the next statement is executed until the end of the program.
3. Select structure the structure is determined based on whether the conditions are established or not, and then decide which statements are to be executed. 3.1. IF statements-single branch structure
If (condition ){
Statement 1;
...
Statement 2;
}Example 1: Compare the size of two Integer Variables
Package pb. test; public class test4 {public static void main (String [] args) {int x = 3; int y = 10; // defines two integer variables, x, y System. out. println ("======= start comparison ========"); if (x> y) {System. out. println ("x is bigger than y! ");} If (x <y) {System. out. println (" x less than y! ");} If (x = y) {System. out. println (" x equals to y! ") ;}System. out. println (" ======== comparison complete ====== ");}}
3.2. if... else statement -- Dual-branch structure
If (condition ){
Statement Subject 1;
} Else {
Statement Subject 2;
}Example 2: Determine whether a number is an odd or even number.
Package pb. test; public class test5 {public static void main (String [] args) {int x = 3; // defines the large and medium-sized variable x if (x % 2 = 0) {// if the remainder is 0, it is an even number of systems. out. println ("x is an even number");} else {// if the remainder is not 0, it is an odd number of systems. out. println ("x is an odd number ");}}}
3.3 when the three-object operator uses the Three-object operator, there are three operands. The format is as follows: Variable = condition judgment? Expression 1: expression 2 determines the expression before or after ":" based on whether the condition is true. If the condition is true, expression 1 is executed, if the value is false, execute expression 2. Example 3. obtain the maximum value of two numbers.
Public static void main (String [] args) {// defines the variable to save the maximum value int max = 0; // defines two variables int x = 3; int y = 10; // use the three-object operation to determine if x> y is used, max = x; otherwise, max = y; max = x> y? X: y; System. out. println ("max:" + max );}
3.4. if... Else if... Else statement -- multi-branch structure if multiple conditions need to be determined in if... else, if... The else statement is in the following format:
If (condition judgment 1 ){
Statement Subject 1;
} Else if (condition Judgment 2 ){
Statement Subject 2;
}
... // Multiple else if () Statements
Else {
Statement subject 3;
}Example 4,
Package pb. test; public class test6 {public static void main (String [] args) {int x = 3; if (x = 1) {System. out. println ("the value of x is 1! ");} Else if (x = 2) {System. out. println (" the value of x is 2! ");} Else if (x = 3) {System. out. println (" the value of x is 3! ");} Else {System. out. println (" the value of x is not one of 2, 3! ");}}}
4. When the Switch structure needs to find and execute one of the statements that meet the judgment conditions in many selection conditions, besides the if .. in addition to constantly judging else, you can also use another more convenient method, that is, multiple options-switch statement, syntax format:
Switch (expression ){
Case Selection value 1: Statement Subject 1; break;
Case Selection value 2: Statement Subject 2; break;
.......
Case Selection value n: statement subject n; break; default: Statement body;
}
4.1,Execution Process
Example 5: Determine the score of a student and give it a score of 90 ~ 100 points, a level greater than 80 points, B level, greater than 70 points, Output C level, greater than 60 points, D level, less than 60 points Output E level
Package pb. test; public class test7 {public static void main (String [] args) {int score = 75; // declare the Student score switch (score/10) {case 10: case 9: system. out. println ("A"); break; case 8: System. out. println ("B level"); break; case 7: System. out. println ("Class C"); break; case 6: System. out. println ("D level"); break; default: System. out. println ("e-level"); break ;}}}
Result: Class C
Or if break is not added;
Package pb. test; public class test7 {public static void main (String [] args) {int score = 75; // declare the Student score switch (score/10) {case 10: case 9: system. out. println ("A"); case 8: System. out. println ("B"); case 7: System. out. println ("Class C"); case 6: System. out. println ("Level D"); default: System. out. println ("e-level ");}}}
Result: Class C, Class D, and Class E