4. Control the execution process and control the execution process
I. the comma operator can define multiple variables in the for statement, but they must be of the same type.
In the control expression initialization and step-by-step Control Section, you can use a series of statements separated by commas, and those statements are executed independently.
Ii. foreach
1 float f[] = new float[10];2 3 for(float x: f){4 //... 5 }
The code above defines a float variable x, and then assigns each f element to x
In addition, foreach can be used for any Iterable
For the following for statement, foreach cannot play a role
1 for(int i = 0; i < 100; i++)
However, the range () function can be used to complete the loop.
1 for(int i : range(10)) 2 3 for(int i : range(5, 10)) 4 5 for(int i : range(5, 20, 3))
3. The range () method has three reloads.
1. range (10) indicates from 0 to 9
2. range (5, 10) indicates from 5 to 9
3. range (5, 20, 3) indicates from 5 to 20. The step value is 3.
4. The break statement indicates that the loop is forcibly exited and the remaining statements are not executed.
The continue statement indicates that the current iteration is stopped, and the next iteration starts at the beginning of the returned loop.
5. goto in Java
1 lable1: 2 outer-iteration { 3 inner-iteration { 4 //... 5 break; 6 //... 7 continue; 8 //... 9 continue lable1;10 //...11 break lable1;12 } 13 }
1. break interrupts the internal iteration and returns to the external iteration.
2. continue enables the execution of point shifting to start at the internal iteration
3. continue label1 interrupts both internal and external iterations and directly transfers them to label1.
4. break label1 also interrupts all iterations and returns to label1, but does not re-enter the iteration.
The only reason to use tags in Java is that there is loop nesting, and you want to use break or continue from multi-layer nesting.