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.