JavaScript While 循环

While 循环
利用 while 循环在指定条件为 true 时来循环执行代码。

while 循环用于在指定条件为 true 时循环执行代码。

语法:

while (变量<=结束值)
{
    需执行的代码
}

注意:除了<=,还可以使用其他的比较运算符。

例子:

var i=0
while (i<=10)
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10
 

Do while 循环
利用 do...while 循环在指定条件为 true 时来循环执行代码。在即使条件为 false 时,这种循环也会至少执行一次。这是因为在条件被验证前,这个语句就会执行。

do...while 循环

do...while 循环是 while 循环的变种。该循环程序在初次运行时会首先执行一遍其中的代码,然后当指定的条件为 true 时,它会继续这个循环。所以可以这么说,do...while 循环为执行至少一遍其中的代码,即使条件为 false,因为其中的代码执行后才会进行条件验证。

语法:

do
{
    需执行的代码
}
while (变量<=结束值)
例子:
var i=0
do 
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
while (i<0)
The number is 0

posted @ 2009-01-07 10:17  CarlZeng  阅读(209)  评论(0编辑  收藏  举报