do...while

do...while 语句创建一个循环,只要测试条件的计算结果为 true,该循环就会执行指定的语句。执行语句后评估条件,导致指定的语句至少执行一次。

¥The do...while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

Try it

语法

¥Syntax

js
do
  statement
while (condition);
statement

至少执行一次的语句,只要条件计算为真,就会重新执行。你可以使用 块语句 来执行多个语句。

condition

每次循环后计算的表达式。如果此条件 评估结果为真,则重新执行 statement。当条件 评估结果为假 时,继续执行 do...while 循环之后的语句。

描述

¥Description

与其他循环语句一样,你可以在 statement 内使用 控制流语句

¥Like other looping statements, you can use control flow statements inside statement:

  • break 停止 statement 执行并转到循环后的第一个语句。
  • continue 停止 statement 执行并重新评估 condition

do...while 语句语法要求末尾有一个分号,但如果缺少分号导致语法无效,自动插入分号 进程可能会为你插入一个分号。

¥The do...while statement syntax requires a semicolon at the end, but the automatic semicolon insertion process may insert one for you if the lack of a semicolon results in invalid syntax.

示例

¥Examples

使用 do...while

¥Using do...while

在以下示例中,do...while 循环至少迭代一次并重复,直到 i 不再小于 5。

¥In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.

js
let result = "";
let i = 0;
do {
  i += 1;
  result += `${i} `;
} while (i > 0 && i < 5);
// Despite i === 0 this will still loop as it starts off without the test

console.log(result);

使用 false 作为 do...while 条件

¥Using false as do...while condition

因为该语句总是执行一次,所以 do...while (false) 与执行语句本身相同。这是 C 类语言中的常见习语,它允许你使用 break 尽早摆脱分支逻辑。

¥Because the statement is always executed once, do...while (false) is the same as executing the statement itself. This is a common idiom in C-like languages, which allows you to use break to break out of branching logic early.

js
do {
  if (!user.loggedIn) {
    console.log("You are not logged in");
    break;
  }
  const friends = user.getFriends();
  if (!friends.length) {
    console.log("No friends found");
    break;
  }
  for (const friend of friends) {
    handleFriend(friend);
  }
} while (false);
// The rest of code

在 JavaScript 中,有一些替代方案,例如将 标记块语句break 一起使用:

¥In JavaScript, there are some alternatives, such as using a labeled block statement with break:

js
handleFriends: {
  if (!user.loggedIn) {
    console.log("You are not logged in");
    break handleFriends;
  }
  const friends = user.getFriends();
  if (!friends.length) {
    console.log("No friends found");
    break handleFriends;
  }
  for (const friend of friends) {
    handleFriend(friend);
  }
}

或者使用函数:

¥Or using a function:

js
function handleFriends() {
  if (!user.loggedIn) {
    console.log("You are not logged in");
    return;
  }
  const friends = user.getFriends();
  if (!friends.length) {
    console.log("No friends found");
    return;
  }
  for (const friend of friends) {
    handleFriend(friend);
  }
}

使用赋值作为条件

¥Using an assignment as a condition

在某些情况下,使用赋值作为条件是有意义的,例如:

¥In some cases, it can make sense to use an assignment as a condition, such as this:

js
do {
  // …
} while ((match = regexp.exec(str)));

但当你这样做时,就需要牺牲可读性。while 文档有一个 使用赋值作为条件 部分,其中包含我们的建议。

¥But when you do, there are readability tradeoffs. The while documentation has a Using an assignment as a condition section with our recommendations.

规范

Specification
ECMAScript Language Specification
# sec-do-while-statement

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also