In the process of a program execution, the execution order of each statement has a direct impact on the result of the program. In other words, the process of the program has a direct impact on the running results. Therefore, we must know the execution process of each statement. Moreover, many times we need to control the execution order of statements to realize the functions we want to complete.
Process control statement classification
Sequential structure
Select structure
Cyclic structure
Sequential structure:
It is the simplest and most basic process control in the program. There is no specific syntax structure. It is executed in sequence according to the sequence of codes. Most codes in the program are executed in this way. In general: those written in the front shall be executed first, and those written in the back shall be executed later
public class Cherris { public static void main(String[] args) { System.out.println("start"); System.out.println("sentence A"); System.out.println("sentence B"); System.out.println("sentence C"); System.out.println("end"); } }
Sequential structure:
start
Statement A
Statement B
Statement C
end
Select structure
Also known as branch structure.
The selection structure has specific syntax rules, and the code needs to perform specific logical operations for judgment. There are two results of logical operations, so the selection is generated, and different codes are executed according to different choices.
The Java language provides two choice structure statements:
if statement
switch Statements
Select structure: if statement
The if statement has three formats
The first format of if statement: if (relational expression)
{
Statement body
}
Execution process
First, judge the relational expression to see whether the result is true or false
If true, execute the statement body
If false, the statement body is not executed
public class Cherris { public static void main(String[] args) { System.out.println("start"); int a=10; int b=15; if (a>b){ System.out.println("a greater than b"); } int c=10; if (a==c){ System.out.println("a be equal to c"); } System.out.println("end"); } }
matters needing attention:
1. Whether the relational expression is simple or complex, the result is boolean.
2.if a statement is controlled by an IF statement, braces can be omitted; If it is more than one statement, it cannot be omitted. It is recommended never to omit it.
3. Generally speaking, there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon.
The second format of if statement: if... else
If (relational expression) {
Statement body 1;
}else
{
Statement body 2;
}
The execution process first judges the relational expression to see whether the result is true or false
If true, execute statement body 1
If false, execute statement body 2
public class Cherris { public static void main(String[] args) { System.out.println("start"); int a=10; int b=15; if (a>b){ System.out.println("a greater than b"); }else { System.out.println("a less than c"); } System.out.println("end"); } }
Odd even:
Enter a number from the keyboard to determine whether it is odd or even
import java.util.Scanner; public class Cherris { public static void main(String[] args) { Scanner xc=new Scanner(System.in); System.out.println(); int number = xc.nextInt(); if (number%2==0){ System.out.println("number:"+"even numbers"); }else { System.out.println("number:"+"Odd number"); } } }
The third format of if statement: if... else} if... If:
Enter the month and confirm the season.
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int month = sc.nextInt(); if (month<1 | month>12){ System.out.println("Wrong number entered"); }else if (month<=5&month>3){ System.out.println("spring"); }else if (month>5&month<=8){ System.out.println("summer"); }else if (month>8&month<=11){ System.out.println("aultume"); }else if (month>11|month<3){ System.out.println("winter"); } } }
There are three ways to compare size: if...... else if...... else ^ if nested ^ implementation of ternary operation
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the first data"); int score1 = sc.nextInt(); System.out.println("Please enter the second data"); int score2 =sc.nextInt(); System.out.println("Please enter the third data"); int score3 = sc.nextInt(); if(score1>score2){ System.out.println("Maximum:"+score1); }else if(score2>score3){ System.out.println("Maximum:"+score2); }else { System.out.println("Maximum value;"+score3); } System.out.println("==============nesting===================="); if(score1>score2){ if(score1>score3){ System.out.println("max"+score1); }else{ System.out.println("max"+score3); } }else{if (score2>score3){System.out.println("max"+score2);} else {System.out.println("max"+score3);} } System.out.println("===============Implementation of ternary operation============="); int a=(score1>score2)?score1:score2; int b=(score3>a)?score3:a; System.out.println("big"+b); } }
switch statement:
1. switch: indicates a selection structure statement
2. Expression:
The expression here must have a result value, but the result data type here can only be byte, short, int, char. After JDK, it can be enumeration, jdk1 7 can be followed by String
3,case:
Matching keywords used in switch
4. Constant value:
The value to be matched, note: here can only be constant value, not variable!!!
5. Statement body:
After matching the corresponding constant value, execute the corresponding statement body content. The statement body can be one or more.
6,break:
Jump out (end) switch selection structure.
7,default:
When all case s do not match the value in the expression, the statement body in default is executed by default.
Execution process:******
1. First, the value in the expression will be calculated
2. Match the constant values in case in turn
3. When matching, execute the corresponding statement body content
4. If all case s do not match, execute the statement body in default finally.
5. When a break occurs or the code is executed, end the switch selection.
matters needing attention:
1. Can break not be written? Yes, but "penetration" will occur“
2. Can default not be written? Yes, but not recommended. The code is not rigorous enough. Under any circumstances, it can not be written. When there are only fixed values, it can not be written
3. Can default be written in the front? If so, what happens?
Yes.
(1) the execution order of default is independent of its position in switch.
(2) when the default order and break ellipsis appear at the same time, the result may not be what you want.
4. The constant values behind multiple case s cannot be the same
import java.util.Scanner; public class Cherris2 { public static void main(String[] args) { //Create keyboard entry object Scanner sc = new Scanner(System.in); System.out.println("Please enter a data (1)-7)"); int number = sc.nextInt(); switch (number){ default: System.out.println("The input data is incorrect"); // break; case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; } } }
Use the penetration of case to find the season of the month:
import java.util.Scanner; public class Cherris { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the month"); int month = sc.nextInt(); switch (month){ case 12: case 1: case 2: System.out.println("winter"); break; case 3: case 4: case 5: System.out.println("spring"); break; case 6: case 7: case 8: System.out.println("spring"); break; case 9: case 10: case 11: System.out.println("spring"); break; default: System.out.println("The number of months entered is incorrect!"); break; } } }