Skip to main content

Understanding do...while Loop in JavaScript

This article is a complementary resource to the Learn JavaScript Basics course.

 

Understanding do...while Loop in JavaScript

The do...while loop is a loop that executes the code block at least once, and then repeats the execution as long as the condition evaluates to true.

It's syntax is:

do {
    // statement(s)
} while (boolean_expression);

Here's how this code works.

1. First, the body of the do...while loop (enclosed in { }) is executed once.

2. Then, if the boolean expression evaluates to

  • true, the body of the loop is executed again.
  • false, the loop terminates.

Let's try one example to understand it better.


Example: The do...while Loop

Here's how you can create a program to print from 1 to 5 using the do...while loop.

let count = 1;

do {
    console.log(`Count = ${count}`);
    count += 1; // Increment count to prevent an infinite loop
} while (count <= 5);

Output

Count = 1
Count = 2
Count = 3
Count = 4
Count = 5

Here's how the above code works in each iteration of the loop.

Iteration Value of count Condition (count <= 5) Body of loop
1 count = 1 1 <= 5 (true) Print Count = 1, increment count to 2.
2 count = 2 2 <= 5 (true) Print Count = 2, increment count to 3.
3 count = 3 3 <= 5 (true) Print Count = 3, increment count to 4.
4 count = 4 4 <= 5 (true) Print Count = 4, increment count to 5.
5 count = 5 5 <= 5 (true) Print Count = 5, increment count to 6.
6 count = 6 6 <= 5 (false) Terminate loop.

Here, the statement count += 1 ensures that count increments by 1 in each iteration. When count reaches 6, the loop terminates because the condition count <= 5 becomes false.


Why Use do...while Over while?

The main advantage of the do...while loop is that it guarantees at least one execution of the block of code, regardless of the initial condition.

Let's look at an example.

// Program to ask user input
// until user enters a negative number

let num = 0;

do {
     // Prompt for user input
    num = parseInt(prompt("Enter a number: "));

// Repeat until a negative number is entered
} while (num > 0);

console.log(`You entered: ${num}`);

Output

Enter a number: 5
Enter a number: 2
Enter a number: -3
You entered: -3

In this scenario, the prompt must appear at least once, regardless of the input.

After that, the loop will repeat only if the condition (num > 0) evaluates to true.

Now, let's see how this differs when using a while loop.

Contrast with The while Loop:

If you use a while loop, the condition is checked before executing the block of code.

let num = 0;

while (num > 0) {
    num = parseInt(prompt("Enter a number: "));
}

console.log(`You entered: ${num}`);

If the initial value of num is negative or 0, the prompt might not appear, as the loop condition would be false initially.

You could adjust the logic, but this is where the do...while loop is particularly useful.


Takeaway

  • Use the do...while loop when you need to ensure the code block is executed at least once.
  • The do...while loop is ideal for scenarios like prompting user input or initializing variables that require at least one attempt.
  • In contrast, a while loop may skip execution entirely if the condition is initially false.