Do while loop run something first then checks the condition if condition is not true then loop exit else it run code block again.
Do while loop Syntax
[code lang=”js”]
do
doing something
while
condition
[/code]
The do while loop is the only loop where a code runs first then it checks if the condition is true or false that means whatever the condition is this loop will have to run at least once. In any condition.
Difference between do while and while loop
The difference between do while and while loops is while loop checks for condition first then runs the code block on other hand do while loop runs a code block first then it checks the condition for another run. You can read more about while loop here.
Example of do while loop
[code lang=”js”]
<script type="text/javaScript">
var counter = 1;
//writing 2 table utp 10
do{
sum = counter*2; //multiply current counter with 2
document.write(‘2 * ‘+counter+’ = ‘+sum+'<br />’);
counter++; //adding 1 number on each run.
} while(counter <= 10)
</script>
[/code]