A. For loop
Grammar:
for (expression one; expression two; expression three) { cyclic operation}
Expression One: Parameter initialization
Expression two: Conditional judgment
Expression three: Updating the loop variable
Execution process:
Step One: Execute expression one. Initialization parameters
Step Two: Execute the expression two the judgment of the cyclic condition
Step three: If the loop condition is true, then the loop operation is performed, and if False The exit loop executes the following code
Step four: Execute expression three when the loop operation finishes, update the loop variable
After the expression is executed, the execution of the expression executes the second loop, and if the loop is true, the following code is executed if False, and so on ...
How to use the For loop structure
(1) To clarify the cycle conditions and cycle operation
(2) Write code for syntax
(3) Check if the loop is able to exit
case: 5 courses recorded from the console to find the average score Public classDemo02 { Public Static voidMain (string[] args) {Scanner input=NewScanner (System.inch); //Total Doublesum=0; for(inti =0; I <5; i++) {System. out. println ("Please enter section"+ (i+1)+"class Entrance results:"); //The average score of single-door results DoubleScore=input.nextdouble (); Sum=sum+score; } //Average score Doubleavg=sum/5; System. out. println ("the average of 5 courses is divided into:"+avg); } }
Two. Break and Continue
1. Use Cases
Break is commonly used in switch structures and loop structures
Continue generally used in cyclic structures
2. function (in the loop structure)
Break statement terminates a loop, and the program jumps to the next statement outside the loop block
Continue jump out of this cycle and into the next cycle
Three. Circular Structure summary
Difference 1: syntax
Difference 2: Execution order
While loop: First judge, then execute
Do-while Cycle: Execute first, then judge
For loop: First judge, then execute
Difference 3: Where applicable
When the number of cycles is determined, the for loop is usually selected
When the number of cycles is uncertain, a while or a do-while loop is usually selected
Small white Java Learning path "cyclic structure (ii)"