Introduction
In JavaScript, a do-while loop serves as a control structure that allows for the repeated execution of a block of code based on a specific Boolean condition. This loop operates similarly to a while loop; however, it has a significant distinction: the do-while loop ensures that the code contained within its block is executed at least one time, irrespective of the truth value of the condition at the start.
There are mainly two types of loops:
Entry-controlled loops: In this category of loops, the condition for testing is evaluated prior to the execution of the loop body. Both for loops and while loops fall under the classification of entry-controlled loops.
Exit-controlled loops: An exit-controlled loop refers to a type of loop in which the condition that determines whether to continue or exit is evaluated after the execution of the loop's body has occurred at least one time. This guarantees that the loop's body is executed a minimum of once. The do-while loop exemplifies an exit-controlled loop.
Syntax
The syntax of the do-while loop in JavaScript is:
do{
//code to be executed
}while(condition)
How does the do-while loop work?
- In a do-while loop, the code block inside the do statement is executed first without checking any conditions.
- Once the code block is executed, the condition in the while statement is evaluated.
- If the condition evaluates to true, the code block is executed again. The loop continues until the condition becomes false.
- If the condition initially evaluates to false, the code block is still executed once before exiting the loop.
Flowchart of do-while Loop
Example
A straightforward program to display a number utilizing a do-while loop.
Example
let i = 1;
do{
console.log(i);
i++;
}while(i<=5)
Output:
1
2
3
4
5
Example
// A program to calculate and display the factorial of a number utilizing a do-while loop in JavaScript
Example
function factorialDoWhile(n) {
let result = 1;
let i = 1;
do {
result *= i;
i++;
} while (i <= n);
return result;
}
const number = 5;
const result = factorialDoWhile(number);
console.log(`The factorial of ${number} is ${result}`); // Output: The factorial of 5 is 120
Output:
The factorial of 5 is 120
Explanation
In this illustration, we create a function referred to as factorialDoWhile that computes the factorial of a given number n. We set the initial value of the result to 1 and the variable i to 1. The do-while loop continuously multiplies the result by i and increases the value of i until it exceeds n. Ultimately, it returns the computed result, and in cases where the input is a negative number, it will provide an error message.
Example
Program to determine whether the specified number is prime or not utilizing a do-while loop.
Example
function isPrime(num) {
if (num < 2) return false; // Numbers less than 2 are not prime
let i = 2;
let isPrime = true;
do {
if (num % i === 0) {
isPrime = false;
break;
}
i++;
} while (i <= Math.sqrt(num)); // Loop runs until the square root of the number
return isPrime;
}
let number = 7;
if (isPrime(number)) {
console.log(number + " is a prime number.");
} else {
console.log(number + " is not a prime number.");
}
Output:
7 is a prime number.
Features of the do-while loop
JavaScript's do-while loop comes with several distinct characteristics. Among these are:
Assured Execution: In JavaScript, the statements contained within a do-while loop are guaranteed to run at least one time, irrespective of the condition's status. This behavior occurs because the condition is assessed after the execution of the code block.
Post-test condition: Within a do-while loop, the evaluation of the condition occurs following the execution of the code block. If the condition for the do-while loop evaluates to true, the loop persists; if it evaluates to false, the loop concludes.
Control flow: In JavaScript, the do-while loop operates as long as the specified condition evaluates to true. To avoid the risk of creating an infinite loop, it is crucial to guarantee that the condition will ultimately resolve to false, thereby preventing endless iterations.
Difference between the while and do-while loop
| Do-while | while |
|---|---|
| The do-while loop is an exit-controlled loop. | The while loop is an entry-controlled loop. |
| The number of iterations will be at least one, irrespective of the condition. | The number of iterations depends upon the condition specified. |
| In a do-while loop, the block of code is controlled at the end. | The block of code is controlled at the start. |