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.

Revision 535329 of do...while

  • Revision slug: Web/JavaScript/Reference/Statements/do...while
  • Revision title: do...while
  • Revision id: 535329
  • Created:
  • Creator: Havvy
  • Is current revision? No
  • Comment

Revision Content

 

Summary

Creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

Version Information

Statement
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262, Edition 3

Syntax

do
   statement
while (condition);

Parameters

statement
A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.
condition
An expression evaluated after each pass through the loop. If condition evaluates to true, the statement is re-executed. When condition evaluates to false, control passes to the statement following the do...while.

Examples

Example: Using do...while

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

do {
   i += 1;
   document.write(i);
} while (i < 5);

See also

while, for

 

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/do...while", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/do...while", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/do...while", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/do...while" } ) }}

Revision Source

<p>&nbsp;</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.</p>
<h2 id="Version_Information">Version Information</h2>
<table class="fullwidth-table">
 <tbody>
  <tr>
   <td class="header" colspan="2">Statement</td>
  </tr>
  <tr>
   <td>Implemented in:</td>
   <td>JavaScript 1.2, NES 3.0</td>
  </tr>
  <tr>
   <td>ECMA Version:</td>
   <td>ECMA-262, Edition 3</td>
  </tr>
 </tbody>
</table>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval">
do
   <em>statement</em>
while (<em>condition</em>);
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>statement</code></dt>
 <dd>
  A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within the loop, use a <a href="/en/JavaScript/Reference/Statements/block" title="en/JavaScript/Reference/Statements/block">block</a> statement (<code>{ ... }</code>) to group those statements.</dd>
</dl>
<dl>
 <dt>
  <code>condition</code></dt>
 <dd>
  An expression evaluated after each pass through the loop. If <code>condition</code> evaluates to true, the <code>statement</code> is re-executed. When <code>condition</code> evaluates to false, control passes to the statement following the <code>do...while</code>.</dd>
</dl>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Using_do...while" name="Example:_Using_do...while">Example: Using <code>do...while</code></h4>
<p>In the following example, the <code>do...while</code> loop iterates at least once and reiterates until <code>i</code> is no longer less than 5.</p>
<pre class="eval">
do {
   i += 1;
   document.write(i);
} while (i &lt; 5);
</pre>
<h3 id="See_also" name="See_also">See also</h3>
<p><a href="/en/JavaScript/Reference/Statements/while" title="en/JavaScript/Reference/Statements/while">while</a>, <a href="/en/JavaScript/Reference/Statements/for" title="en/JavaScript/Reference/Statements/for">for</a></p>
<p>&nbsp;</p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/do...while", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/do...while", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/do...while", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/do...while" } ) }}</p>
Revert to this revision