JavaScript loop statements while, Do-while, for-in, for usages distinguish _javascript techniques

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)

<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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

For in instance two
<ptml> <body> <script type= "Text/javascript" > var x var mycars = new Array () mycars[0] = "BMW" Myca RS[1] = "Benz" mycars[2] = "Bentley" for (x in Mycars) {document.write (mycars[x] + "<br>")} </script> </b Ody> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Javascrpt for
<script type= "Text/javascript" > for (i = 0; I <= 5; i++) {document.write ("number is" + i) document.write ("<br& gt; ")} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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:
<ptml> <pead> <title> A JavaScript example that uses the Do...while loop </title> </pead> <body> < p> <script type= "Text/javascript" > i = 0 do {document.write (i + ' <br> ') i++} while (I <= 5) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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
<script type= "Text/javascript" > i = 0 while (i <= 5) {document.write ("number is" + i) document.write ("<br> ") i++} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.
JavaScript Sample Code Explanation: 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>
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.