您的位置:首页 > Web前端 > JavaScript

JavaScript While 循环

2009-01-07 10:17 309 查看
While 循环
利用 while 循环在指定条件为 true 时来循环执行代码。while 循环用于在指定条件为 true 时循环执行代码。

语法:

[code]while
(变量<=结束值)
{
需执行的代码
}
[/code]
注意:除了<=,还可以使用其他的比较运算符。例子:

var i=0[code]while (i<=10)
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}

[/code]
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,因为其中的代码执行后才会进行条件验证。

语法:

[code]do

{
需执行的代码
}
while
(变量<=结束值)
[/code]
例子:


var i=0[code]do
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
while (i<0)

[/code]
[code]The number is 0

[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: