The use of JavaScript in the loop

Source: Internet
Author: User

Learning website:

Http://www.w3school.com.cn/js/js_loop_for.asp

JavaScript Loops

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.

We can output the values of the array like this:

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 usually we write this:

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

Try it yourself.

Different types of loops

JavaScript supports different types of loops:

    • For-loop code block for a certain number of times
    • For/in-Looping through the properties of an object
    • While-loops the specified block of code when the specified condition is true
    • Do/while-also loops the specified block of code when the specified condition is true
For loop

A For loop is a tool that you will often use when you want to create loops.

Here is the syntax for the FOR loop:

for (statement 1; Statement 2; Statement 3)  {  code block executed  }

Statement 1 executes before the Loop (code block) starts

Statement 2 defining conditions for running loops (code blocks)

Statement 3 executes after a loop (code block) has been executed

Instance
For (Var i=0, i<5; i++)  {  x=x + "The number is" + i + "<br>";  }

Try it yourself.

From the example above, you can see:

Statement 1 Sets the variable (var i=0) before the loop begins.

Statement 2 defines the conditions for the loop to run (I must be less than 5).

Statement 3 Adds a value (i++) after each code block has been executed.

Statement 1

Typically we use statement 1 to initialize the variable (var i=0) used in the loop.

Statement 1 is optional, that is, you do not use statement 1.

You can initialize any (or more) of the values in statement 1:

Instance:
For ( var i=0,len=cars.length; i<len; i++) {document.write (Cars[i] + "<br>");}

Try it yourself.

You can also omit statement 1 (such as when a value has been set before the loop starts):

Instance:
var i=2,len=cars.length;for (; i<len; i++) {document.write (Cars[i] + "<br>");}

Try it yourself.

Statement 2

Typically statement 2 is used to evaluate the condition of the initial variable.

Statement 2 is also optional.

If statement 2 returns True, the loop starts again, and if False is returned, the loop ends.

Tip: If you omit statement 2, you must provide a break within the loop. Otherwise, the loop cannot be stopped. This may cause the browser to crash. Read about break in the section later in this tutorial.

Statement 3

Usually statement 3 increases the value of the initial variable.

Statement 3 is also optional.

Statement 3 has several uses. The increment can be negative (i--), or larger (i=i+15).

Statement 3 can also be omitted (for example, when there is a corresponding code inside the loop):

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

Try it yourself.

For/in Cycle

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

Instance
inperson)  {  txt=txt + person[x];  }

Try it yourself.

You'll learn more about the For/in loop in the chapters on JavaScript objects.

The use of JavaScript in the 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.