JavaScript--Looping statements

Source: Internet
Author: User

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.

Grammar:

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

While loop

The while loop executes a code block when the specified condition is true.

Grammar:

 while (condition) {    code to execute}
Do/while Cycle

The Do/while loop is a variant of the while loop. The loop executes the block of code once before checking whether the condition is true, and then repeats the loop if the condition is true.

Grammar:

 Do {    the code to execute}while (condition);

Demo

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8">        <title>Looping statements</title>    </Head>    <Body>        <Scripttype= "Text/javascript"src= "Test.js" ></Script>        <Buttononclick= "loop ()">Looping statements</Button>    </Body></HTML>

JS inside the Loop statement logic code:

/** * Loop statement*/functionLoop () {document.write ("----------for Loop traversal-----------" + "<br>"); Cars= ["BMW", "Volvo", "Saab", "Ford"];  for(vari = 0; i < cars.length; i++) {document.write (Cars[i]+ "<br>"); } document.write ("---------------------" + "<br>"); vari = 0, Len=cars.length;  for(; I <Len;) {document.write (Cars[i]+ "<br>"); I++; } document.write (The----------for/in statement loops through the properties of the object-----------"+" <br> "); varx; vartxt = ""; varperson ={fname:"Bill.", lname:"Gates", Age:56            }; //object traversal through for             for(xinchPerson ) {                //string concatenation, via +TXT = txt +Person[x]; } document.write (txt+ "<br>"); document.write ("----------while loop statement----------" + "<br>"); varx = "", I= 0;  while(I < 5) {x= x + "This number is" + i + "<br>"; I++; } document.write (x+ "<br>"); document.write ("----------Do While loop statement----------" + "<br>"); varx = "", I= 0;  Do{x= x + "The number is" + i + "<br>"; I++; }             while(I < 5) document.write (x+ "<br>");}

JavaScript--Looping statements

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.