JavaScript Loops

CodingTute

JavaScript

A loop is a control structure that allows you to execute a block of code multiple times. There are 3 types of loops as follows:

  • for loop
  • while loop
  • do-while loop

for loop

A for loop is a control structure that allows you to execute a block of code a specific number of times. It is defined using the for keyword, followed by a set of parentheses that contain three optional expressions, separated by semicolons:

for (initialization; condition; increment) {
  // code to be executed
}

Here is an example of a for loop:

for (let i = 0; i < 10; i++) {
  console.log(i);
}

In this example, the for loop will execute the code inside the curly braces 10 times, with the value of i starting at 0 and incrementing by 1 each time. The loop will stop when i is equal to 10.

The three expressions in the parentheses are as follows:

  1. initialization: This expression is executed before the loop starts. It is often used to initialize a loop counter variable. In the example above, the initialization expression is let i = 0, which declares a variable i and assigns it a value of 0.
  2. condition: This expression is evaluated before each iteration of the loop. If it evaluates to true, the loop continues; if it evaluates to false, the loop stops. In the example above, the condition expression is i < 10, which checks if the value of i is less than 10.
  3. increment: This expression is executed after each iteration of the loop. It is often used to update the loop counter variable. In the example above, the increment expression is i++, which increments the value of i by 1.

You can use a for loop to iterate over an array or an object, or to perform a certain task a specific number of times. Here is an example of using a for loop to iterate over an array:

let colors = ['red', 'green', 'blue'];

for (let i = 0; i < colors.length; i++) {
  console.log(colors[i]);
}

In this example, the for loop iterates over the elements in the colors array and prints each one to the console. The loop stops when i is equal to the length of the array.

while loop

A while loop is a control structure that allows you to execute a block of code as long as a certain condition is true. It is defined using the while keyword, followed by a set of parentheses that contain a condition:

while (condition) {
  // code to be executed
}

Here is an example of a while loop:

let i = 0;

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

In this example, the while loop will execute the code inside the curly braces as long as the value of i is less than 10. The loop will stop when i is equal to 10.

The condition in the parentheses is evaluated before each iteration of the loop. If it evaluates to true, the loop continues; if it evaluates to false, the loop stops.

It’s important to make sure that the condition in the while loop eventually becomes false, or else the loop will run indefinitely and create an infinite loop. You can use a counter variable to track the number of iterations, or you can use a flag variable to signal when the loop should stop.

You can use a while loop to iterate over an array or an object, or to perform a certain task until a certain condition is met. Here is an example of using a while loop to iterate over an array:

let colors = ['red', 'green', 'blue'];
let i = 0;

while (i < colors.length) {
  console.log(colors[i]);
  i++;
}

In this example, the while loop iterates over the elements in the colors array and prints each one to the console. The loop stops when i is equal to the length of the array.

do-while loop

A do...while loop is a control structure that allows you to execute a block of code as long as a certain condition is true. It is similar to a while loop, but it guarantees that the code inside the loop will be executed at least once.

A do...while loop is defined using the do and while keywords, followed by a set of curly braces that contain the code to be executed and a set of parentheses that contain a condition:

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

Here is an example of a do...while loop:

let i = 0;

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

In this example, the do...while loop will execute the code inside the curly braces at least once, and then it will continue to execute the code as long as the value of i is less than 10. The loop will stop when i is equal to 10.

The condition in the parentheses is evaluated after each iteration of the loop. If it evaluates to true, the loop continues; if it evaluates to false, the loop stops.

You can use a do...while loop to iterate over an array or an object, or to perform a certain task until a certain condition is met. Here is an example of using a do...while loop to iterate over an array:

let colors = ['red', 'green', 'blue'];
let i = 0;

do {
  console.log(colors[i]);
  i++;
} while (i < colors.length);

In this example, the do...while loop iterates over the elements in the colors array and prints each one to the console. The loop stops when i is equal to the length of the array.

You can find the complete JavaScript Tutorials here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Exit mobile version