1. Process Control
1.1 Overview
The flow of the program has a direct impact on the running result, so we must be clear about the execution flow of each statement.
Process: During the execution of a program, the execution order of each statement. Flow control: The function we want to accomplish is realized by controlling the execution order of the statements.
Program flow chart:
Also known as a program block diagram, it is a graphic representation that describes the specific steps of a program with uniformly specified standard symbols.
The design of the program block diagram is based on the processing flow chart, through the detailed analysis of the input and output data and the processing process, to identify the main operating steps and contents of the computer.
Flowchart standard notation:
1.2 Sequence structure
The execution flow chart is as follows:
example:
public class Demo01Sequence{ //The meaning of Sequence public static void main(String[] args){ System.out.println("the weather is nice today!"); System.out.println("Very windy and sunny!"); System.out.println("We have no class this afternoon!"); System.out.println("This is really cool!"); }}
2. Judgment Statement
2.1 Single if statement
The execution flow chart is as follows:
example:
public class Demo02SIf{ public static void main(String[] args){ System.out.println("the weather is nice today! Is pressing the road - suddenly saw a happy place: Internet cafes!"); int age = 160; if(age >= 18){ System.out.println("Enter the Internet cafe and start hi!"); System.out.println("I met a group of pig teammates and started to scold!"); System.out.println("Feel bad, check out and leave!"); } System.out.println("Home for dinner!"); }}
2.2 Standard if-else statement
The execution flow chart is as follows:
example:
public class Demo03SIfElse{ public static void main(String[] args){ int num = 30; if(num % 2 == 0){//If the remainder is 0 when divided by 2, it is an even number. System.out.println("even"); } else{ System.out.println("odd number"); } }}
2.3 Extending the if-else statement
The execution flow chart is as follows:
example:
// The relationship between x and y is as follows: // x>=3 y = 2x + 1; //‐1<=x<3 y = 2x; // x<=‐1 y = 2x – 1; public class Demo04SIfElseExt{ //ext extension meaning public static void main(String[] args){ int x = -10; int y = 2; //Unassigned content cannot be used if(x >= 3){ y = 2*x + 1; //2x is wrong in the program, it should be 2*x. } else if(-1 <= x && x <3){ y = 2*x; }else{ y = 2*x - 1; } System.out.println(x); System.out.println("turn out:" + y); //Pay attention to this spelling }}
Sentence practice:
/* Specify test scores and judge student grades 90-100 excellent 80-89 it is good 70-79 good 60-69 Pass 60 The following fail */ public class Demo05SIfElsePractise{ public static void main(String[] args){ int score = 69; if(score < 100 && score >= 90){ System.out.println("Fraction:" + score + " excellent"); }else if(score < 90 && score >= 80){ System.out.println("Fraction:" + score + " it is good"); }else if(score < 80 && score >= 70){ System.out.println("Fraction:" + score + " good"); }else if(score < 70 && score >=60){ System.out.println("Fraction:" + score + " Pass"); }else{ //Note why nesting is used if(score > 100 || score < 0){ System.out.println("Fraction:" + score + " mistake"); } else{ System.out.println("Fraction:" + score + " failed"); } } }}
2.4 Interchange of if statement and ternary operator
Standard if-else statement, choose one of the two; the ternary operator can also do it.
example:
//Title: Use the ternary operator and the standard if-else statement to achieve: take the maximum of two numbers public class Demo06Max{ public static void main(String[] args){ int x = 100; int y = 20; int z = x >= y ? x : y; System.out.println("The ternary operator gets the maximum value:" + z); System.out.println(" "); if(x >= y){ System.out.println("standard if-else The statement gets the maximum value:" + x); }else{ System.out.println("standard if-else The statement gets the maximum value:" + y); } } }
3. Select Statement
3.1 Select Statement - Switch
The execution flow chart is as follows:
example:
public class Demo07Switch{ public static void main(String[] args){ int num = 70; //Detected amount switch(num){ case 1: System.out.println("today is Monday");break; case 2: System.out.println("Today is tuesday");break; case 3: System.out.println("Today is Wednesday");break; case 4: System.out.println("Today is thursday");break; case 5: System.out.println("today is Friday");break; case 6: System.out.println("today is Saturday");break; case 7: System.out.println("today sunday");break; default: System.out.println("mistake!");break; //It is strongly recommended not to omit the break of default. } }}
3.2 Notes
Notes on using the switch statement:
- The values following multiple case s cannot be repeated.
- The [detected value] in the parentheses after switch can only be of the following data types:
4 basic data types: byte/short/char/int
2 reference data types: String string, enum enumeration - The format of the Switch statement can be very flexible: the sequence can be reversed, and the break statement can be omitted; whichever case is matched, it will be executed from which position until it encounters a break or ends as a whole.
example:
public class Demo08SwitchNotice{ public static void main(String[] args){ int num = 3; switch(num){ case 3:System.out.println("Hello"); //Without break, it will penetrate down and continue to execute the following code. case 1:System.out.println("I'm good");break; default:System.out.println("He is fine, so am I");break;//default is not necessarily the last, it is recommended not to omit break. It only makes sense at the end. case 2:System.out.println("Hello everyone");break; } }}
4. Loop Statement
4.1 Overview of the cycle
Loop statement: When a loop condition is met, a certain piece of code is repeatedly executed, and this piece of code that is repeatedly executed is called a loop body statement.
When the loop body is repeatedly executed, it is necessary to modify the loop judgment condition to false at the appropriate time to end the loop, otherwise the loop will continue to execute, forming an infinite loop.
4.2 for loop
The execution flow chart is as follows:
The basic components of the loop structure can generally be divided into four parts:
1. Initialization statement: Executed initially at the beginning of the loop, and only once.
2. Conditional judgment: if it is established, the loop continues; if not, the loop exits.
3. Loop body: Repeat the things to be done, several lines of statements.
4. Step-by-step expression statement: The finishing work that needs to be done after each loop is executed once after the end of each loop body.
example:
public class Demo09For{ public static void main(String[] args){ System.out.println("I was wrong!"); System.out.println("I was wrong!"); System.out.println("I was wrong!"); for(int index = 0;index < 100; index++){ //index index System.out.println("the first" + (index+1) + "Times: I was wrong!"); } }}
4.3 while loop
The while loop has a standard form and an extended form.
standard format:
while(Conditional judgment){ loop body }
Extended format:
initialization statement; while(Conditional judgment){ loop body; step expression statement; }
The execution flow chart is as follows:
example:
public class Demo10While{ public static void main(String[] args){ int i = 0; while(i<100){ System.out.println("the first" + (i+1) + "Times: I was wrong!"); i++; } }}
4.4 do-while loops
There are two formats for do-while loops:
standard format:
do{ loop body }while(Conditional judgment);
Extended format:
initialization statement; do{ loop body statement; step expression statement; }while(control statement);
The extended format execution flow chart is as follows:
4.5 The difference between the three loops
The difference between the three loops:
1. If the condition is never met, the for loop and the while loop will be executed 0 times, but the do-while loop will be executed at least once.
2. The variables of the for loop are defined in parentheses and can only be used inside the loop. The while loop and do-while loop initialization statements are already outside, so they can be used after the loop ends.
example:
public class Demo13LoopDifference{ public static void main(String[] args){ for(int i = 0;i < 0; i++){ System.out.println("Hello! Xiao Zhang"); } //System.out.println(i);//Wrong writing, i is not defined yet int i = 0; do{ System.out.println("Hello! little king"); i++; }while(i < 0); System.out.println(i); }}
4.6 Loop Control (Bounce) Statements
break keyword:
There are two common usages of the break keyword:
1. Can be used in a switch statement. Once executed, the entire switch statement ends immediately.
2. It can also be used in a loop statement. Once executed, the entire loop will end immediately and the loop will be interrupted.
Regarding the choice of loop, there is a small suggestion:
For scenes with a certain number of times, use for loops, otherwise use while loops; do-while loops are not commonly used.
example:
public class Demo14break{ public static void main(String[] args){ for(int i = 1;i < 10;i++){ //If it starts from the fourth time, the follow-up is not needed, and the cycle is interrupted System.out.println("Hello! Xiao Zhang" + i); if(i == 4){ break; } } }}
continue keyword:
Another loop control statement is the continue keyword:
Once executed, immediately skip the remaining contents of the current loop (usually placed before the loop body), and start the next loop immediately.
example:
public class Demo15Continue{ public static void main(String[] args){ for(int i = 1;i <= 10;i++){ if(i == 4){ continue; } System.out.println(i + "The floor is here"); } }}
5. Expand knowledge points
5.1 Infinite loop
Infinite loop: A loop that never stops is an infinite loop.
There are generally two situations:
- Keep an endless loop.
- Heartless endless cycle.
The standard format for an infinite loop:
while(true){ loop body; }
example:
public class Demo16DeadLoop{ //DeadLoop public static void main(String[] args){ for(int i = 1;i <= 10;i++ ){ //i++ forgot to write. System.out.println(i + "The floor is here"); } while(true){ System.out.println("I Love You!"); } //System.out.println("!!!!!!");//Inaccessible statement }}
5.2 Loop Nesting
Nested loops: It means that the body of one loop is another loop. for example for There is one more in the loop for A loop is a nested loop. total number of cycles=Number of outer loops * Number of inner loops.
example:
public class Demo17LoopHourAndMinute{ public static void main(String[] args){ for(int hour = 0; hour < 24;hour++){ //Outer Control Hours for(int minute = 0; minute < 60;minute++){ //Inner control minutes for(int second = 0; second < 60;second++){ System.out.println(hour + ":" + minute +":" + second); } } } }}
practise:
Question: Find the sum of even numbers between 1 and 100. Find sums between 1–100 and odd sums.
Ideas:
- Now that the range has been determined to be 1–100, I go from 1, 2, 3... all the way up to 100 to check one by one.
- There are a total of 100 numbers, not all numbers can be used, they must be even numbers, judge (if statement) even numbers: num % 2 == 0.
- A variable is required for the accumulation operation. That is, a piggy bank.
public class Demo12HundredSun{ public static void main(String[] args){ int sum1 = 0; int sum2 = 0; int sum3 = 0; for(int i1 = 1;i1 <= 100;i1++){ if(i1 % 2 == 0){ sum1 += i1; } } int i2 = 1; while(i2 <= 100){ if(i2 % 2 != 0){ sum2 += i2; } i2++; } int i3 = 0; do{ sum3 += i3; i3++; }while(i3 <= 100); System.out.println("0--100 even and yes" + sum1); System.out.println("0--100 odd and yes" + sum2); System.out.println("0--100 and yes" + sum3); }}
-This document is a study note!