Some things in JavaScript for loops

Source: Internet
Author: User

Today in a learning girl to tune her code bug, her problem is not clear in a for loop flow of the specific process, so that caused the page how to tune is a problem, well, if you do not know the statement flow is very easy to go wrong, so say for the loop will not use or use of inappropriate or very scary. I would also take this opportunity to review the for () loop in JavaScript in passing. I've turned out some of my previous notes about the For loop, some of which are floating in my mind, and most of the notes from a long time ago may be part of some of the other great blogs on the web.

There are three process statements in the For loop, with ";" between the three sections. Separate. The first is an assignment statement , which is used to assign an initial value to the loop control variable. Second, a conditional expression is a relational expression that determines when to exit a loop. Third, increment defines how the loop control variable changes after each cycle.

Next down is a struct {}, which contains the code you want to execute. This is probably what it looks like: for(assigning an initial value statement; loop condition; increment) {statement;}

Then the order of execution for the For loop is as follows.

1. Assignment statement: Execute only once, run backward to next
2. Cycle conditions: Used to determine whether the condition is set up, if set up to proceed to the next step, do not set out the entire cycle
3. Contents of the Loop body: if there is a break, do not execute the code below and jump out of the loop. If you have continue, skip the code after continue and proceed to the next step.
4. Increment value: Used to calculate the variable.

If there is no break or continue statement in the For loop, the general end loop exits the loop after judging the relationship expression if the condition is not met.

Theoretically, as long as the external conditions are sufficient, the three process statements in the For loop can beomitted., is not a little frightened (pretending to be surprised by the face):

1. When the outer part has a variable used within the definition loop, omitting the definition in the For loop, the external global variable is used as the variable within this for loop. So you often see that there is a for loop that defines a global variable first:

2. The loop condition can theoretically be omitted when the FOR loop inner statement has a break that satisfies what condition.

3. Recursive increment, can also be defined within the for loop structure.

So when one day you see a for loop like this structure, please promise me not to be surprised for (;;) { ... }

Well, there seems to be nothing to say. That's the first thing.

Right, finally I learned to use the above those add the gaudy things, later blog content will be more and more wonderful Oh, think of a little excited about it.

End.

Some things in JavaScript for loops

Related Article

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.