Following the last section, we have finished talking about the branch structure, and our part B is going to talk about the loop structure. Please savor the key points below
Article directory
a loop statement
Two for loops
Three while loops
Four do-while loops
Five comparisons of three cyclic structures
Six nested loops (or multiple loops)
The use of seven break and continue
Eight Scanner: Realization of keyboard input function
Nine summary
foreword
We have talked about the if-else statement and the switch statement, and then we have the turn of the loop structure, what's new, please continue to read
a loop statement
Loop structures are: for loop, whlie loop and do while loop
And our loop structure has four elements: initialization part, loop condition part, loop body part iteration part
Two for loops
For loop format: initialization part 1, loop condition part 2, loop body part 3, sub-iteration part, 4
for (1 2 3 ){
4
}
Cycle sequence: 1234234234.....2
focus:
- There is a semicolon in for (;;), neither more nor less
- 1 The initialization part can declare multiple variables, but they must be of one type, separated by commas
- 2 The loop part is an expression of boolean type, when the value is false, exit the loop
- 4 There can be multiple variable updates, separated by commas
public class test{
public static void main(String[] args){
for(int i = 1;i <= 5;i++){
System.out.println("我叫北辰")
}
}
}
We can use break in the loop, once the break is executed, it will jump out of the current loop
So how should we end the loop structure? There are two situations: the loop condition in the loop structure returns false, or a break is executed in the loop structure. If a loop structure cannot end, then it is an infinite loop. When should the code avoid this kind of thing from happening?
Three while loops
The format of the while loop: initialization part 1, loop condition part 2, loop body part 3, iteration part 4
1
while(2){
3;
4;
}
Cycle process: 1234234234...2
illustrate:
- The loop condition in while must be boolean
- Among them, we must not forget the iterative part of four, otherwise it will become an infinite loop
- for and while can be converted to each other, and the scope of the initial conditions of for and while loops is different
Four do-while loops
The format of the do-while loop: initialization part 1, loop condition part 2, loop body part 3, iteration part 4
1
do{
3
4
}while 2 ;
Cycle process: 134234234..........2
illustrate:
- The loop condition in the end while (loop condition) must be boolean type
- The do-while loop body statement will be executed at least once
- The for, while, and do-while in the loop body can be converted to each other
Five comparisons of three cyclic structures
All of them need initialization part 1, loop condition part 2, loop body part 3, and iteration part 4. These four parts are composed.
Among them, do-while will be executed at least once, while for and whlie first judge whether the loop condition statement is true, and then decide whether to execute the loop body.
So many representatives of the cycle, so how should we choose?
- When there is an obvious requirement for the number of cycles, use a for loop
- If there are no obvious looping requirements, you can use while
- A do-while loop if the body of the loop needs to be run at least once
- Therefore, in essence, the three can be converted to each other, and all can realize the function of circulation.
Six nested loops (or multiple loops)
The so-called nested loop: means that the loop body of a loop structure A is another loop structure B. For example, there is another for loop inside the for loop, which is a nested loop. Among them, for, while, and do-while can be used as outer loops or inner loops.
Outer loop: loop structure A Inner loop: loop structure B
In essence, a nested loop is a loop body that treats the inner loop as the outer loop. Only when the loop condition of the inner loop is false, will the inner loop be completely jumped out, the current outer loop can be ended, and the next outer loop can be started.
Assuming that the number of loops in the outer layer is m times and the number of loops in the inner layer is n times, the inner loop body actually needs to be executed m*n times.
Tip: From the point of view of two-dimensional graphics, the outer loop controls the number of rows and the inner loop controls the number of columns.
Development experience: In actual development, the most nested loops we see are two layers. Generally, there will not be more than three levels of nested loops. If it will appear, we must stop and reorganize the business logic, rethink the implementation of the algorithm, and control it within three layers. Otherwise, readability will be poor.
For example: two for nested loop format
for (1; 2; 7) {
for (3, 4, 6) { 5 } }
Cycle characteristics: the outer layer is cycled once, and the inner layer is cycled once
The use of seven break and continue
The use of break:
- The scope of application is in the switch-case statement. Once executed, the current loop structure will end. After this keyword, no statement can be declared
Use of continue:
- In the loop structure, once it is executed, the loop structure will end. After this keyword, no statement can be declared
Eight Scanner: Realization of keyboard input function
How to get variables of different types (basic data type, String type) from the keyboard: use the Scanner class.
Four steps for keyboard input code:
1. Import package: importjava.util.Scanner;
2. Create an object of Scanner type: Scannerscan=new
Scanner(System.in);
3. Call related methods of the Scanner class (next()/ nextXxx()) to obtain the variable of the specified type
4. Release the resource: scan.close();
Note: It is necessary to input the value of the specified type according to the corresponding method. If the input data type
does not match the required type, an exception will be reported and the program will terminate.
Nine summary
This is all our loop structure part, I divided it into two chapters, next we will explore the Java (4) array part, after the array is completed, we will talk about the Java (5) object-oriented chapter in detail, it is estimated It will take a long time, I hope everyone will continue to pay attention, thank you