Control Structure (I), control structure (
The role of the control structure is to control the execution sequence of statements in a program. It is the key to the structured program design. The basic principle of structured program design is "top-down, gradually refined". It can be roughly divided into four types: sequential structure, selection structure, cyclic structure, and jump structure.
First, we will introduce the sequence structure and selection structure.
1. Ordered Structure
The sequential structure is the simplest and most basic process control. In the ordered structure, as long as the corresponding statements are written in the order of solving the problem, the program runs each statement from top to bottom in sequence. The structure is as follows:
Statement 1: -----> Statement 2 ------> Statement 3 -----> ......
Code:
public class Test{public static void main(String[] args){int a = 1;int b = 3;int c = a + b;System.out.println("c="+c);}}
2. Select Structure
The java language provides two basic selection structure statements: if statement and switch statement. The two statements can form a selection structure in the form of 4.
1. if statement
The if statement is a single branch selection structure, which processes certain conditions accordingly. The specific syntax format of the if statement is as follows:
If (condition expression) {statement block ;}
If the condition is true, the statement in the block is executed. Otherwise, the block is skipped and other statements following the block are directly executed. Example:
public class Test{public static void main(String[] args){int x = 10;int y = 5;if(x>y){System.out.println("x>y");}if(x<y){System.out.println("x<y");}}}
Note: 1.if statement cannot be followed by a plus sign. Otherwise, the code block after the if statement is always executed no matter whether the conditions are met or not.
2. If only one statement in the statement block can be used, "{}" is not used.
2. if-else statement
The if-else statement is a dual-Branch selection structure. The if-else statement is used to determine the value of a conditional expression. When the value is true, an operation is executed. if the value is false, another operation is executed. The if-else syntax is as follows:
If (condition expression) {statement Block 1;} else {statement Block 2 ;}
Description: True execution statement 1, false execution statement 2. Example:
public class Test{ public static void main(String[] args){ int x = 10; int y = 5; if(x>y){ System.out.println("x>y"); }else{ System.out.println("x<y"); } }}
3. nested if statements
For complex cases, use nested if-else statements. Syntax:
If (condition expression 1) {statement Block 1;} else if condition expression 2 () {statement Block 2 ;}... else if (condition expression n) {statement Block n;} else {statement Block n + 1 ;}
Execution Process: if the value of a conditional expression is true, execute the statement blocks following it, and the rest are ignored. If all the values are false, execute the statement blocks following the last else. Example:
public class Test{ public static void main(String[] args){ int score = 85; char grade; if(score>=90){ System.out.println("A+"); }else if(score>=80){ System.out.println("A"); }else if(score>=70){ System.out.println("B"); }else if(score>=60){ System.out.println("C"); }else{ System.out.println("D"); } }}
4. switch statement
When you want to select one branch from multiple branches for execution, although the if nested statement can be used for implementation, when there are many branches in the program, the readability of the program will be greatly reduced, in this case, you can use the switch statement to solve the problem of multi-branch selection. The switch statement determines which of the multiple operations to execute based on the value of the test expression. The syntax is as follows:
Switch () {case value 1: Statement 1; break; case value 2: Statement 2; break ;... case value n: Statement n; break; [default: Statement Block n + 1;]}
Note: 1. the type of the test expression after the switch must be byte, char, short, and int.
2. values 1 to n must be constants of the same type as the test expression, and their values should be different from each other. Otherwise, there will be two methods for executing the same value in multiple ways.
3. The statement block after case can be enclosed in parentheses.
4. the break statement function is that after the program executes the selected branch, it can jump out of the entire switch statement. If there is no break statement, the program will not make any judgment, execute the case statement block after him until the break statement is met or n + 1 is executed.
5. The default statement is optional.
6. switch statements cannot replace all nested if statements. This is because the condition of the switch structure is a value, while the condition of the if-else structure can be a range.
The switch statement is as follows:
Import java. util. upload; // import the upload package. You can enter public class Test {public static void main (String [] args) {synchronized SC = new synchronized (System. in); System. out. println ("enter a 1 ~ Integer between 7 "); int day = SC. nextInt (); switch (day) {case1: System. out. println ("Monday"); break; case2: System. out. println ("Tuesday"); break; case3: System. out. println ("Wednesday"); break; case4: System. out. println ("Thursday"); break; case5: System. out. println ("Friday"); break; case6: System. out. println ("Saturday"); break; case7: System. out. println ("Sunday"); break; default: System. out. println ("Listen 8 ");}}}
You can also merge case values, for example:
Switch (month) {case1: case3: case5: case7: case8: case10: case12: System. out. println ("31 days"); break; case4: case6: case9: case11: System. out. println ("30 days"); break; case2: System. out. println ("28 or 29 days"); default: System. out. println ("Incorrect !!! ");}