JavaScript Basics Learning--for Loops

Source: Internet
Author: User

For loop syntax:

For ( statement 1; statement 2; Statement 3)
{
code block to be executed
}

Execute starts before the start of statement 1 (code block). Optional, can be default. You can initialize any (or more) values here, as well as omit statement 1 (such as when a value has been set before the loop starts).

Statement 2 defines the conditions for running loops (code blocks). Optional, can be default. If statement 2 returns True, the loop starts again, and if False is returned, the loop ends. If statement 2 is omitted, then a break must be provided within the loop. Otherwise, the loop cannot be stopped.

Statement 3 executes after a loop (code block) has been executed. Optional, default (e.g. when there is code inside the loop).

Example:

<!DOCTYPE HTML><HTML><Body><Script>Cars=["BMW","Volvo","Saab","Ford"]; for (varI=0; I<Cars.length;i++) {document.write (Cars[i]+ "<br>");}</Script></Body></HTML>

When I=0, I<4, enters the loop, at this time prints "BMW", then I++,i=1;

When I=1, i<4, enter the cycle, at this time print "Volvo", and then i++,i=2;

When i=2, I<4, enters the loop, at this time prints "Saab", then i++,i=3;

When I=3, I<4, enters the cycle, at this time prints "Ford", then i++,i=4;

When i=4, exit the loop.

The value has been set before the loop starts:

var i=2,len=cars.length;  for (; i<len; i+++ "<br>");}

The loop is written inside the For loop:

var i=0,len=cars.length;  for (; i<+ "<br>"); I+ +;}

For/in Cycle

The JavaScript for/in statement loops through the properties of the object:

var person={fname: "John", lname: "Doe", Age:25};  for inch Person )  {  txt=txt + person[x];  }

JavaScript Basics Learning--for Loops

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.