Differences in the usage of javascript loop statements while, do-while, for-in, and

Source: Internet
Author: User

The only difference between the first two is that the order of loop and judgment is different. do-while is more than while, and I will not give an example.
For loop I believe everyone is familiar with it, so let's look at the for-in sentence.
This is actually for arrays, And the array initialization in js is also quite strange, for example, we write in the script node :( also pay attention to the array initialization, using brackets)

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
For in instance 2
<Html> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Javascrpt

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.
Explanation:
The step value of the for loop starts from I = 0.
As long as I is less than or equal to 5, the loop continues to run.
Once every cycle of a loop, I will accumulate 1.
Javascrpt do while:
<Html> <pead> <title> A Javascript example using do... while loop </title> </pead> <body> <p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.
Explanation:
I is equal to 0.
The loop runs first.
I will accumulate 1 for each loop.
When I is less than or equal to 5, the loop continues to run.
Javascrpt while

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The number is 0.
The number is 1.
The number is 2.
The number is 3.
The number is 4.
The number is 5.
Explanation:
I is equal to 0.
When I is less than or equal to 5, the loop continues to run.
Once a loop is run, I will accumulate 1.
Javascript sample code explanation: This Javascript example uses the do... while loop statement.
Loop statements allow repeated execution of one or several lines of code. do is followed by the code that is repeated, while is followed by the condition that the loop is terminated. In this Javascript example, set a variable to I, and the initial I value to 0. I ++ indicates that the value of I is added to 1 after each repeated execution, the condition for terminating the loop is while (I <= 5). That is, once the I value is greater than 5, the loop is terminated. In this example, the statement for repeating the loop is the document. write statement in the while loop. </P>
From the above examples, we can see the differences between js for, for in, while, And do while.

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.