Article

The do...while Loop

https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js
Copy to clipboard

The do...while loop is similar to the while loop except that the condition check happens at the end of the loop.

This means that the loop will always be executed at-least once, even if the condition is false.

In this example we write the condition so that it always evaluates to false to illustrate the workings of the do...while loop https://raw.githubusercontent.com/finsweet/jqueryCourse/main/loops/ex3.js?actionable=true

In this example the output of the web page will be:CurrentCount : 0

This is the output because the script executes the document.write() and increases the count by 1 before checking if the count is less than 0. Since this condition evaluates to false, the while loop will end with just one execution.

<script>
$(document).ready(function(){
  XT1
});
</script>
Copy to clipboard
Send to Live Demo

Live demo

<div id="minidivpage" class="example-live"><div id="resultdiv"></div></div>

<script>    
$(document).ready(function() {
  XT1
});
</script>
<script>
  
</script>
Copy to clipboard
Press the Run Demo button to execute the scripts above.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Your browser is too small

Resize your browser to be at least 769px wide.