English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
do...whileStatement creates a loop that executes the specified statement until the calculation result of the test condition is false.
Condition(condition)Evaluate after executing the statement, resulting in the specified statement being executed at least once.
JavaScript provides the following types of loops:
for - The loop traverses the code block for several times
for...in - Traverse the properties of the object
while - The loop traverses the code block when the specified condition is true
do...while - The loop executes a code block once and then repeats the loop when the specified condition is true
UsebreakStatement terminates the current loop and usescontinueStatement skips the value in the loop.
do { //Executed statements } while (condition);
var n = 0; do { document.write("<br>The number is " + n); n++; } while (n < 5);Test and See‹/›
Note:If you want to use aCondition(condition)Variables, please initialize them before the loop, and then increment them within the loop. If you forget to increase the variable, the loop will never end. This will cause your browser to crash.
All browsers fully support the do ... while statement:
Statement | |||||
do...while | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
condition | Expression evaluated after each iteration of the loop. If the condition evaluates to true, the statement is re-executed. When the condition evaluates to false, control is passed to the statement after do ... while. If the condition is always true, the loop will never end. This can cause your browser to crash. |
JavaScript Version: | ECMAScript 1 |
---|
Even if the condition is false, this loop will execute at least once because the code block is executed before the condition test:
var n = 5; do { document.write("<br>The number is " + n); n++; } while (n < 3); // falseTest and See‹/›
JavaScript Reference:JavaScript while Statement
JavaScript Reference:JavaScript break Statement
JavaScript Reference:JavaScript continue Declaration