Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

do...while

概览

do...while 语句创建了一个循环,该循环执行一个指定的语句直到condition 的值为 false. condition 在执行statement后才会被赋值,statement至少执行一次.

语法

do
   statement
while (condition);
statement
至少执行一次的语句,且只要condition值为true时,会再次执行. 想执行多行语句,可使用block语句({ ... }包裹这些语句 .
condition
循环中每次都会计算的表达式. 如果其值为true,statement会再次执行.当其值为false,则跳到 do...while 之后的语句.

示例

例子: 使用 do...while

下面的例子中, do...while 循环至少迭代一次,并且继续迭代直到 i 不再小于5时结束.

var i = 0;
do {
   i += 1;
   console.log(i);
} while (i < 5);

规范

Specification Status Comment
ECMAScript 3rd Edition. Standard Initial definition.
Implemented in JavaScript 1.2
ECMAScript 5.1 (ECMA-262)
do-while statement
Standard  
ECMAScript 6 (ECMA-262)
do-while statement
Release Candidate Trailing ; is now optional.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) IE6+ (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

相关链接

文档标签和贡献者

 此页面的贡献者: yenshen
 最后编辑者: yenshen,