JavaScript do while Loop

In this chapter, we will learn about the JavaScript do...while loop. The do...while loop is used to execute a block of code at least once, and then repeat the execution as long as a specified condition is true. We will cover:

  • What is a do...while Loop?
  • Syntax
  • Using the do...while Loop
  • Comparison with while Loop
  • Breaking and Continuing in do...while Loops
  • Simple Programs using do...while Loops

What is a do…while Loop?

The do...while loop is a control flow statement that allows code to be executed at least once and then repeatedly based on a given boolean condition. It is similar to the while loop, but with a key difference in the order of execution and condition checking.

Syntax

do { // code to be executed } while (condition); 
  • condition: The condition to be evaluated after each iteration. If true, the loop continues. If false, the loop ends.

Using the do…while Loop

Example

let i = 0; do { console.log(i); i++; } while (i < 5); 

Output:

0 1 2 3 4 

In the example above, the loop prints the value of i and increments i by 1, then checks if i is less than 5. The loop stops when i reaches 5.

Comparison with while Loop

The key difference between while and do...while loops is that while loops check the condition before executing the code block, whereas do...while loops execute the code block at least once before checking the condition.

while Loop Example

let i = 5; while (i < 5) { console.log(i); i++; } 

Output:

(no output) 

do...while Loop Example

let i = 5; do { console.log(i); i++; } while (i < 5); 

Output:

5 

In the example above, the do...while loop executes the code block once even though the condition is false.

Breaking and Continuing in do…while Loops

The break statement is used to exit a loop early, while the continue statement is used to skip the current iteration and continue with the next iteration.

Example with break

let i = 0; do { if (i === 5) { break; } console.log(i); i++; } while (i < 10); 

Output:

0 1 2 3 4 

Example with continue

let i = 0; do { i++; if (i === 5) { continue; } console.log(i); } while (i < 10); 

Output:

1 2 3 4 6 7 8 9 10 

Simple Programs using do…while Loops

Program 1: Print Numbers from 1 to 10

let i = 1; do { console.log(i); i++; } while (i <= 10); 

Output:

1 2 3 4 5 6 7 8 9 10 

Program 2: Calculate the Sum of the First 10 Natural Numbers

let sum = 0; let i = 1; do { sum += i; i++; } while (i <= 10); console.log("Sum:", sum); 

Output:

Sum: 55 

Program 3: Find the Factorial of a Number

let number = 5; let factorial = 1; let i = 1; do { factorial *= i; i++; } while (i <= number); console.log("Factorial:", factorial); 

Output:

Factorial: 120 

Program 4: Print the Elements of an Array

let fruits = ["apple", "banana", "mango"]; let i = 0; do { console.log(fruits[i]); i++; } while (i < fruits.length); 

Output:

apple banana mango 

Program 5: Reverse a String

let str = "hello"; let reversedStr = ""; let i = str.length - 1; do { reversedStr += str[i]; i--; } while (i >= 0); console.log("Reversed String:", reversedStr); 

Output:

Reversed String: olleh 

Conclusion

In this chapter, you learned about the JavaScript do...while loop, including its syntax, how to use it, and the differences between while and do...while loops. We also covered breaking and continuing in do...while loops and provided various use cases with simple programs to demonstrate the usage of do...while loops. The do...while loop is used for repeating tasks that need to be executed at least once before checking a condition.

Leave a Comment

Scroll to Top