JavaScript While 循环-CarlZeng

While 循环利用 while 循环在指定条件为 true 时来循环执行代码。while 循环用于在指定条件为 true 时循环执行代码。语法:while (变量”)i=i+1}The number is 0The number is 1The number is 2The number is 3The number is 4The number is 5The number is 6The n…

While 循环

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

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

语法:

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

语法:

1
do

例子:

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

1
The number is 0