English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript-Anweisungen und Variablen-Deklarationen
do...whileThe statement creates a loop that executes the specified statement until the 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 iterates over the code block several times
for...in - Iterate over the properties of an object
while - The loop iterates over 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
UsebreakThe statement terminates the current loop and usescontinueThe statement 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);Testen Sie heraus‹/›
Note:If you want to use aCondition (condition)Initialize the variable before the loop, and then increment it 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 | Beschreibung |
---|---|
condition | Ausdruck, der nach jedem Durchlauf des Loops bewertet wird. Wenn die Bedingung true ist, wird die Anweisung erneut ausgeführt. Wenn die Bedingung false ist, wird die Kontrolle an die Anweisung nach do ... while übergeben. Wenn die Bedingung immer true ist, wird der Loop niemals enden. Dies könnte Ihren Browser zum Absturz bringen. |
JavaScript-Version: | ECMAScript 1 |
---|
Selbst wenn die Bedingung false ist, wird dieser Loop mindestens einmal ausgeführt, da der Codeblock vor der Bedingungstest ausgeführt wird:
var n = 5; do { document.write("<br>The number is " + n); n++; } while (n < 3); // falseTesten Sie heraus‹/›
JavaScript Referenz:JavaScript while-Anweisung
JavaScript Referenz:JavaScript break-Anweisung
JavaScript Referenz:JavaScript continue-Deklaration