The only difference between the first two is that the order of looping and judgment is different. do-while loops one more time than while, so I won’t give an example.
I believe everyone is familiar with the for loop. Let’s look at the sentence for-in.
This is actually for arrays. The initialization of arrays in js is also quite strange. For example, we write it in the script node: (Also note the initialization of the array, using square brackets)
]
for in Example 2
If you need to introduce external Js, you need to refresh to execute
]
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh 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 will continue to run.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh 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 will run first.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh 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 will continue running when i is less than or equal to 5. Every time the loop runs, i will accumulate by 1. Javascript sample code explanation: This Javascript sample uses the do...while loop statement. The loop statement allows one or several lines of code to be repeatedly executed. do is followed by the code for repeated execution, and while is followed by the condition for terminating the loop. In this Javascript example, let a variable be i, the initial value of i is 0, i means that the value of i will be increased by 1 after each repeated execution, and the loop termination condition is while (i <= 5), that is to say, once i If the value is greater than 5, the loop will be terminated. In this example, the statement that repeats the loop is the document.write statement in the while loop. From the above examples we can see the difference between js for, for in, while, do while.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn