tnk*_*nkh 5 javascript for-loop
在JS中,我偶然发现了一种for循环,for(;;)就像while(true)循环一样.分号在for循环的括号中起什么作用?
for (statement 1; statement 2; statement 3) {
code block to be executed
}
Run Code Online (Sandbox Code Playgroud)
语句1是可选的,在循环(代码块)开始之前执行.
var i = 0;
var length = 10
for (; i < length; i++) {
//The for loop run until i is less than length and you incerement i by 1 each time. javascript doesnt care what you do inside, it just check whether you have variable with name i and length
}
Run Code Online (Sandbox Code Playgroud)
语句2再次是可选的,定义了运行循环的条件(代码块).
var i = 0;
var len = 100;
for (i = 5; ; i++) {
//Here you are just initializing i with 5 and increment it by 1 there is no break condition so this will lead to an infinite loop hence we should always have a break here somehwere.
}
Run Code Online (Sandbox Code Playgroud)
语句3是可选的,每次执行循环(代码块)后执行.
var i = 0;
var length = 100;
for (; i < length; ) {
//Here you are just checking for i < length which is true. If you don't increment i or have an break it will turn into infinite loop
}
Run Code Online (Sandbox Code Playgroud)
在没有条件或初始化的情况下,在坚果壳中,它变成无限循环.
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |