JavaScript do / while statement
JavaScript Statements Reference Manual
Examples
The loop executes at least once, even if the condition is false, because the code block is executed before the conditional statement judgment:
var text = "";
var i = 0;
do {
text + = "The number is" + i;
i ++;
}
while (i <5);
var i = 0;
do {
text + = "The number is" + i;
i ++;
}
while (i <5);
text output is:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 1
The number is 2
The number is 3
The number is 4
try it"
Definition and Usage
do / while loop is a variant of the while loop. The loop will execute a block of code before checking whether the condition is true, and if the condition is true, it will repeat the cycle.
JavaScript support different types of loops:
- for - loop can execute a block of code a specified number of times.
- for / in - loop through the properties of an object
- the while - when a specified condition is true cycle specified block of code
- do / while - also when the specified condition is true cycle specified code blocks, but the cycle will determine the conditions before a code block execution
Browser Support
Statements | |||||
---|---|---|---|---|---|
do / while | Yes | Yes | Yes | Yes | Yes |
grammar
do {
code block to be executed
}
while (condition);
code block to be executed
}
while (condition);
Parameter Value
parameter | description |
---|---|
condition | have to. Define the execution condition for the loop. If true, the loop will execute again if it returns false, the loop ends. Note: If the condition has been true, the loop will not end (infinite loop). This will make your browser crash. Note: If you use a variable as a criteria, before the loop starts to initialize variables, and let it grow in circulation since, if you forget to set the variable self growth, an infinite loop condition will also appear the same browser will crash. |
technical details
JavaScript version: | 1.2 |
---|
Related Pages
JavaScript Tutorials: JavaScript the While loop
JavaScript Reference Manual: JavaScript the while statement
JavaScript Reference Manual: JavaScript for statements
JavaScript Statements Reference Manual