JavaScript(JS) while Loop

JavaScript while Loop

In JavaScript, a while loop is another way to execute a block of code repeatedly until a specified condition is met. The basic syntax of a while loop is as follows:

refer‮:ot ‬theitroad.com
while (condition) {
  // Code to be executed
}

Here's what the while loop syntax means:

  • condition: This is the condition that is checked before each iteration of the loop. If the condition is true, the loop continues to execute. If it is false, the loop stops.

Let's look at an example. Suppose you want to print the numbers 1 to 10 to the console using a while loop. Here's how you can do it:

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

In this example, we're using a while loop to iterate over the numbers 1 to 10. The let i = 1 statement initializes the loop counter to 1. The i <= 10 condition checks if the loop counter is less than or equal to 10. The i++ statement increments the loop counter by 1 at the end of each iteration.

You can use the break statement to exit a while loop prematurely, and the continue statement to skip the rest of the current iteration and move on to the next iteration.

let i = 1;
while (i <= 10) {
  if (i === 5) {
    break; // Exit the loop when i equals 5
  }
  if (i % 2 === 0) {
    i++; // Increment i when i is even
    continue; // Skip the rest of the code in the current iteration
  }
  console.log(i);
  i++; // Increment i at the end of each iteration
}

In this example, we're using the break statement to exit the loop when i equals 5. We're using the continue statement to skip the rest of the code in the current iteration when i is even. This means that only the odd numbers from 1 to 4 will be printed to the console. Note that we're also incrementing i at the end of each iteration, even when i is even, to avoid an infinite loop.

JavaScript do...while Loop

In JavaScript, a do...while loop is similar to a while loop, but it executes the code block at least once before checking the condition. The basic syntax of a do...while loop is as follows:

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

Here's what the do...while loop syntax means:

  • code to be executed: This is the code that is executed at least once before checking the condition.
  • condition: This is the condition that is checked after each iteration of the loop. If the condition is true, the loop continues to execute. If it is false, the loop stops.

Let's look at an example. Suppose you want to print the numbers 1 to 10 to the console using a do...while loop. Here's how you can do it:

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

In this example, we're using a do...while loop to iterate over the numbers 1 to 10. The let i = 1 statement initializes the loop counter to 1. The console.log(i) statement prints the current value of i to the console. The i++ statement increments the loop counter by 1 at the end of each iteration. The while (i <= 10) condition checks if the loop counter is less than or equal to 10.

Note that even if the initial condition is false, the code block will still execute at least once before checking the condition. You can use the break statement to exit a do...while loop prematurely, and the continue statement to skip the rest of the current iteration and move on to the next iteration.

let i = 1;
do {
  if (i === 5) {
    break; // Exit the loop when i equals 5
  }
  if (i % 2 === 0) {
    i++; // Increment i when i is even
    continue; // Skip the rest of the code in the current iteration
  }
  console.log(i);
  i++; // Increment i at the end of each iteration
} while (i <= 10);

In this example, we're using the break statement to exit the loop when i equals 5. We're using the continue statement to skip the rest of the code in the current iteration when i is even. This means that only the odd numbers from 1 to 4 will be printed to the console. Note that we're also incrementing i at the end of each iteration, even when i is even, to avoid an infinite loop.