Looping statements while, Do-while, for-in, for usage differences in JavaScript

Source: Internet
Author: User

The first two only difference is that the loop and the order of the judgments are different, do-while than while more than one cycle, I do not give an example.

For loop I believe that we can not be ripe, we will see for-in this sentence.

This is actually for the array, JS array initialization is also very strange, such as we write in the script node: (also note the initialization of the array, with the brackets)

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
document.write ("test<br/>");
var a=[3,4,5,7];
for (var test in a) {
document.write (test+ ":" +a[test]+ "<br/>");
}
-->
</script>

For in instance two

The code is as follows Copy Code

<body>
<script type= "Text/javascript" >
var x
var mycars = new Array ()
Mycars[0] = "BMW"
MYCARS[1] = "Mercedes"
MYCARS[2] = "Bentley"

for (x in Mycars)
{
document.write (Mycars[x] + "<br/>")
}
</script>
</body>

Javascrpt for

The code is as follows Copy Code

<script type= "Text/javascript" >
for (i = 0; I <= 5; i++)
{
document.write ("number is" + i)
document.write ("<br/>")
}
</script>


The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.

Explain:
The step value for the For loop starts at i=0.

As long as I is less than or equal to 5, the loop will continue to run.

Loop every time, I will add 1.


javascrpt Do While:

The code is as follows Copy Code
<title> A JavaScript example that uses the Do...while loop </title>
<body>
<p>
<script type= "Text/javascript" >
i = 0
Todo
{
document.write (i + "<br>")
i++
}
while (I <= 5)
</script>

The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.

Explain:
I equals 0.

The loop will run first.

Once per cycle, I will accumulate 1.

When I is less than or equal to 5 o'clock, the loop continues to run.


Javascrpt while

The code is as follows Copy Code

<script type= "Text/javascript" >
i = 0
while (I <= 5)
{
document.write ("number is" + i)
document.write ("<br/>")
i++
}
</script>

The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.

Explain:
I equals 0.

When I is less than or equal to 5 o'clock, the loop continues to run.

Every time the loop runs, I will accumulate 1.

</p>
<p>javascript Example code explains: This JavaScript example uses the Do...while loop statement.
A loop statement allows you to repeat a line or lines of code, followed by a repeating code, while followed by a condition that terminates the loop. In this JavaScript example, set a variable to the I,i initial value of 0,i++ to add 1 to the value of I after each repetition, terminating the loop condition as while (I <= 5), that is, to terminate the loop once I has a value greater than 5. In this example, a repeating loop statement is a document.write statement inside a while loop. </p>
</body>

From the above example, we can see the difference between JS For,for in, while and do.

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.