English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript do...while statement

 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.

Syntax:

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.

Browser compatibility

All browsers fully support the do ... while statement:

Statement
do...whileisisisisis

Parameter value

ParameterBeschreibung
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.

Technische Details

JavaScript-Version:ECMAScript 1

Mehr Beispiele

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);  // false
Testen Sie heraus‹/›

Zusammenhängende Referenzen

JavaScript Referenz:JavaScript while-Anweisung

JavaScript Referenz:JavaScript break-Anweisung

JavaScript Referenz:JavaScript continue-Deklaration

 JavaScript-Anweisungen und Variablen-Deklarationen