While 循环
只要指定条件为 true,循环就可以一直执行代码。
语法:
while (条件)
{
需要执行的代码
}
下面通过一个例子来说明
js代码
var i=0;
while ( i<5)
{
var x= "The number is "+ i;
i++;
console.log(x);
}
输出为:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
do/while 循环
do/while 循环是 while 循环的变体。该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环。
语法:
do
{
需要执行的代码
}
while (条件);
同样也通过一个例子来说明
js代码
do
{
x="The number is "+ i;
i++;
console.log(x);
}
while (i<5);
执行后的结果:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
通过两个例子可以看出While 循环和do/while 循环的效果一样。

被折叠的 条评论
为什么被折叠?



