Tutorial
Do While Loop

Do-while Loops in JavaScript

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Syntax of Do-while Loop

Do-while Loops Syntax Breakdown

do {
  // code block to be executed
} while (condition);

The do-while loop will execute the code block once before checking the condition. If the condition is true, the code block will be executed again. This process will repeat until the condition becomes false.

👀

But remember do while loop at least execute once.

Here is an example of a do-while loop in JavaScript:

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

⭐ Explaining Step by Step of Do-While Loop

Do-While Loops Explained with Example

💥 Do-While Loop Example

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

💥 Visualize the Example

1. i = 0
2. print i
3. i++
4. if i < 3, go back to step 2
5. if i >= 3, end loop

💥 Visualize the Example

  • The variable i is initialized to 0. This sets the starting value for the loop counter.
  • The condition of the do-while loop is checked. Since this is the first iteration, the condition is not checked yet.
  • The code block of the do-while loop is executed. This prints the value of i (0) to the console.
  • The value of i is incremented by 1. This updates the loop counter.
  • The condition of the do-while loop is checked. Since the value of i is now 1, which is less than 3, the condition is true.
  • The code block is executed again. This prints the value of i (1) to the console.
  • The value of i is incremented by 1. This updates the loop counter.
  • The condition of the do-while loop is checked. Since the value of i is now 2, which is less than 3, the condition is true.
  • The code block is executed again. This prints the value of i (2) to the console.
  • The value of i is incremented by 1. This updates the loop counter.
  • The condition of the do-while loop is checked. Since the value of i is now 3, which is not less than 3, the condition is false.
  • The do-while loop is terminated and execution continues with the next line of code.

⚡ Playground

⭐ Example of Do-while Loop

Do-while Loops Example for Beginners

✨ 1. Printing the numbers from 1 to 10:

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

✨ 2. Printing the numbers from 10 to 1:

let i = 10;
do {
console.log(i);
i--;
} while (i >= 1);

✨ 3. Printing the even numbers from 1 to 10:

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

✨ 4. Printing the odd numbers from 1 to 10:

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

✨ 5. Calculating the sum of the first 10 numbers:

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

✨ 6. Calculating the sum of the first 10 even numbers:

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

✨ 7. Calculating the sum of the first 10 odd numbers:

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

✨ 8. Printing the numbers from 1 to 10 in reverse:

let i = 10;
do {
console.log(i);
i--;
} while (i >= 1);

📚 Extra

😊

To create an infinite loop with a do-while loop in JavaScript, you can use the following

let i = 0;
do {
// code to be executed
i++;
} while (i < 10);

To create an infinite loop, you can simply omit the condition, like this:

let i = 0;
do {
// code to be executed
i++;
} while (true);

Keep in mind that an infinite loop can be very dangerous, as it will run indefinitely and could potentially crash your program. Make sure you include a way to exit the loop, such as a user input or a countdown timer, to prevent unintended consequences.