Small white Java Learning path "cyclic structure (ii)"

Source: Internet
Author: User

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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.