aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/statements/do...while/index.html
blob: 89a850f7222e786e50327d6618a250d47ddf6d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: do...while
slug: Web/JavaScript/Reference/Statements/do...while
tags:
  - JavaScript
  - Statement
translation_of: Web/JavaScript/Reference/Statements/do...while
---
<div>
<div>{{jsSidebar("Statements")}}</div>
</div>

<p><strong><code>do...while</code> 语句</strong>创建一个执行指定语句的循环,直到<code>condition</code>值为 false。在执行<code>statement</code> 后检测<code>condition</code>,所以指定的<code>statement</code>至少执行一次。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox">do
   <em>statement</em>
while (<em>condition</em>);
</pre>

<dl>
 <dt><code>statement</code></dt>
 <dd>执行至少一次的语句,并在每次 <code>condition</code> 值为真时重新执行。想执行多行语句,可使用{{jsxref("Statements/block", "block")}}语句(<code>{ ... }</code>)包裹这些语句。</dd>
</dl>

<dl>
 <dt><code>condition</code></dt>
 <dd>循环中每次都会计算的表达式。如果 <code>condition</code> 值为真, <code>statement</code> 会再次执行。当 <code>condition</code> 值为假,则跳到<code>do...while</code>之后的语句。</dd>
</dl>

<h2 id="Examples" name="Examples">示例</h2>

<h3 id="Example:_Using_do...while" name="Example:_Using_do...while">使用 <code>do...while</code></h3>

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

<h3 id="HTML_内容">HTML 内容</h3>

<pre class="brush: html line-numbers  language-html"><code class="language-html">&lt;div id="example"&gt;&lt;/div&gt;</code></pre>

<h3 id="JavaScript_内容">JavaScript 内容</h3>

<pre class="brush: js"><code class="language-js">var result = '';
var i = 0;
do {
   i += 1;
   result += i + ' ';
} while (i &lt; 5);
document.getElementById('example').innerHTML = result;</code></pre>

<h3 id="结果">结果</h3>

<p>{{ EmbedLiveSample('Examples') }}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.6.1', 'do-while statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-do-while-statement', 'do-while statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Trailing ; is now optional.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-do-while-statement', 'do-while statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>



<p>{{Compat("javascript.statements.do_while")}}</p>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li>{{jsxref("Statements/while", "while")}}</li>
 <li>{{jsxref("Statements/for", "for")}}</li>
</ul>