The use of the For loop in JavaScript-basic knowledge

Source: Internet
Author: User

We've seen that while loops have different variants. This chapter will introduce another popular loop called A For loop.
For loop

The For loop is the most compact form of the loop and consists of the following three important parts:

    1. The initial value of the loop initialization counter. Before the initialization statement execution loop begins.
    2. A test statement that tests if a given condition is true or false. If the condition is true, the code given in the loop that will be executed, otherwise the loop exits.
    3. Loop statement, you can increase or decrease the counter.

You can separate the lines in all three sections with semicolons.
Grammar

for (initialization. test condition; Iteration statement) {
   statement (s) to was executed if test condition is true
}

Example:

The following example illustrates a basic for loop:

<script type= "Text/javascript" >
<!--
var count;
document.write ("Starting Loop" + "<br/>");
for (count = 0, Count < count++) {
 document.write ("Current count:" + count);
 document.write ("<br/>");
}
document.write ("Loop stopped!");
-->
</script>

This produces the following result, similar to the while loop:

Starting Loop current
count:0 current count:1 current count:2 current
-count:3 current
Count:  4 Current
count:5 current Count:6 current Count:7 current Count:8 current
count:9
Loop stopped! 

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.