JavaScript Basic Series (vi) loop statement (for Loop)

Source: Internet
Author: User

If you want to run the same code over and over again, and the values are different each time, it is convenient to use loops.

document.write (Cars[0] + "<br>");d ocument.write (cars[1] + "<br>");d Ocument.write (cars[ 2] + "<br>");d ocument.write (cars[3] + "<br>");d ocument.write (cars[4] + "<br > ");d ocument.write (cars[5] +" <br> ");

But we write this.

 for (var i=0; i<cars.length;i++) {document.write (Cars[i]+ "<br>");}

Example: Output 1-100 number

 for (var i=0;i <=100;i++) {document.write (i+ "<br>")}

For is a pre-test loop, and you can initialize the variable before the loop, and define the code to execute after the loop, with the following syntax

for (inintialization;expression;psot=loop-expression) statement

The following procedures are performed:

1. Execute initialization statement

2. Determine if expression is true and if yes, continue, or terminate the entire loop body.

3. Executing the Loop Body statement code

4. Execute post-loop-expression Code

5. Return to the 2nd step operation

The most common form for a For loop is for (var i=0; i<n;i++) {statement}

It indicates that the loop executes a total of n times and is well suited for known loop count operations.

JavaScript Basic Series (vi) loop statement (for Loop)

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.