对于(;;)循环说明 [英] For (;;) loop explanation
本文介绍了对于(;;)循环说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在JS中,我偶然发现了一种for循环,该循环是 for(;;)
,其功能类似于 while(true)
循环.分号在此for循环的括号中起什么作用?
In JS I stumbled across a kind of for loop which is for(;;)
that functions like a while(true)
loop. What do the semicolons function in the brackets of this for loop?
推荐答案
for (statement 1; statement 2; statement 3) {
code block to be executed
}
语句1是可选的,并且在循环(代码块)开始之前执行.
Statement 1 is optional and is executed before the loop (the code block) starts.
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
}
语句2也是可选的,它定义了运行循环(代码块)的条件.
Statement 2 is again optional defines the condition for running the loop (the code block).
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.
}
语句3是可选的,并且在每次执行循环(代码块)之后都会执行.
Statement 3 is optional and is executed each time after the loop (the code block) has been executed.
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
}
在坚果壳中,如果没有条件或没有初始化,它将变成无限循环.
这篇关于对于(;;)循环说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文