do...while loop in JavaScript works like while loop in JavaScript the only difference is that do block executes at least once before while loop’s test condition. That’s why do block executes whether it satisfies the while condition or not.

Syntax

do
{
    // statements to be executed
}
while([condition]);

statements: the code statements that are executed at least once and then for each condition evaluated to true.

condition: expression that is to evaluated to decide whether to execute the next iteration of loop. If true, the loop is re-executed otherwise it terminates the loop.

Example

var i=1;
do
{
    document.write('<font size=\"' + i + '\">Hello World</font><br />');
    i++;
}
while(i<0);

Above example will display the “Hello World” string once before the while condition i<0 is being tested.

No comments yet.

Leave a Comment

All fields are required. Your email address will not be published.

Recent JavaScript Tutorials

JavaScript if-else Ternary Operator

The JavaScript conditional ternary operator is considered as shorthand for if-else statement that is frequently used to return the result in single code statement. It takes three operands to return ...

JavaScript do…while Loop

do...while loop in JavaScript works like while loop in JavaScript the only difference is that do block executes at least once before while loop’s test condition. That’s why do block ...

JavaScript while Loop

while loop in JavaScript works same like for loop in JavaScript but in a little bit different manner. while loop runs until the condition is true and exits when the ...

JavaScript for Loop

for loop in JavaScript is most often used to execute a block of code with different value each time. So, to reduce the code length instead of adding number of ...

JavaScript switch-case Statement

To execute a single block of code satisfying a condition in case statement of JavaScript switch-case is used. break keyword is used in every case statement to exit the switch ...

Recent Comments