JavaScript do-while Loop

JavaScript do while loop; In this tutorial, you will learn javaScript do while loop with help of syntax, flowchart and examples.

JavaScript do-while Loop

  • Introduction JavaScript do while Loop
  • Syntax of the do while Loop
  • Flowchart of do while loop
  • Example 1: First JavaScript do while loop
  • Example 2: JavaScript do while loop with Break Statement

Introduction JavaScript do while Loop

The JavaScript  do-while  loop is also known as an exit control loop. The JavaScript do-while is test specified condition after executing a block of code. If the condition met false, at least once execute the block of code.

Syntax of the do while Loop

do{
   statement(s);
 } while(expression);

The do-while loop always executes the block of code least once before it check specified condition.

Because the condition is checked only after the block of code executes once. The do-while loop is called a post-test/exit control loop.

You have known above, the do while loop is known as an exit control loop. For this reason, it is possible that the block of code inside the do while loop is executed at least once, if the specified condition met false.

Flowchart of do while loop

do while loop flowchart

Example 1: Print 1 to 10 numbers in javascript using do while loop

See the following example of the do-while loop.

<script>

let count = 0;
do {
    count++;
    document.write(count + "<br>")
} while (count < 10);

</script>

How the script works:

  • First, define count variable and assign value to it.
  • Next, the do while loop checks specified condition after execute block of code, if count is less than 10 and execute the statements inside the loop body.
  • Next, in each do while loop iteration, increments count by 1.And After 10 iterations, the condition count < 10 met false and loop will be terminate.

The output of the script shown below:

1
2
3
4
5
6
7
8
9
10

Example 2: JavaScript do while loop with Break Statement

See the following example of the do-while loop.

<script>

    let count = 0;
    do {

        if(count == 5)
        break;

        count++;
        document.write(count + "<br>")
    } while (count < 10);


</script>

How the script works:

  • First, define count variable and assign value to it.
  • Next, the do while loop checks specified condition after executing block of code, if count is less than 10 and execute the statements inside the loop body.
  • Next, in each do while loop iteration, increments count by 1. Using a break statement, stop the execution of loop After 5 iterations.

The output of the script shown below:

1
2
3
4
5

Conclusion

In this tutorial, you have learned what is do-while  loop and how to use the do-while loop.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *