for Loop

'for' Loop

The `for` loop is a control flow statement that allows you to repeatedly execute a block of code a specific number of times. It is commonly used when you know beforehand how many times the loop should iterate. The `for` loop consists of three main parts: initialization, condition, and increment/decrement.

Syntax

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

}

The `initialization` statement is executed before the loop starts and is used to initialize variables or set their initial values. The `condition` is evaluated before each iteration, and if it evaluates to `true`, the loop body is executed. The `increment/decrement` statement is executed after each iteration and is generally used to update the loop counter.

Example

Let's look at an example that demonstrates the usage of a `for` loop to print the numbers from 1 to 5:

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

In this example, the `initialization` statement `let i = 1` initializes the loop counter `i` to 1. The `condition` `i <= 5` checks if `i` is less than or equal to 5. If it is true, the loop body is executed, which logs the value of `i` to the console. The `increment/decrement` statement `i++` increases the value of `i` by 1 after each iteration. The loop continues as long as the condition is true, and in this case, it will print the numbers 1 to 5.

Break Statement

Inside a `for` loop, you can use the `break` statement to terminate the loop prematurely. This can be useful when you want to stop the loop based on a certain condition instead of waiting for the loop condition to become `false`.

Here's an example that demonstrates the usage of `break` statement within a `for` loop:

for (let i = 1; i <= 10; i++) {
  if (=== 6) {
    break;
  }
  console.log(i);
}

In this example, the loop will iterate from 1 to 10. However, when the loop counter `i` becomes 6, the `break` statement is encountered, and the loop is terminated, even though the condition `i <= 10` is still true. As a result, only the numbers 1 to 5 will be logged to the console.

Continue Statement

The `continue` statement is another control flow statement that allows you to skip the rest of the code inside the loop for the current iteration and move on to the next iteration.

Here's an example that demonstrates the usage of `continue` statement within a `for` loop:

for (let i = 1; i <= 5; i++) {
  if (=== 3) {
    continue;
  }
  console.log(i);
}

In this example, when the value of `i` is 3, the `continue` statement is encountered, and the rest of the loop body is skipped for that iteration. As a result, the number 3 is not logged to the console, and the loop continues with the next iteration.

Nested `for` Loops

The `for` loop can also be nested, which means you can have one `for` loop inside another `for` loop. This is especially useful when you need to iterate over a multi-dimensional array or when you want to perform a certain action for every combination of two or more variables.

Here's an example that demonstrates the usage of nested `for` loops to print a pattern:

for (let i = 1; i <= 5; i++) {
  let pattern = '';
  for (let j = 1; j <= i; j++) {
    pattern += '*';
  }
  console.log(pattern);
}

In this example, the outer `for` loop is responsible for controlling the number of rows, while the inner `for` loop is responsible for controlling the number of columns in each row. The `pattern` variable is used to build the string pattern for each row, adding an asterisk symbol `*` in each iteration of the inner loop. The result is a pattern of asterisks, where each row has an increasing number of asterisks.

Key Takeaways

  • The `for` loop allows you to repeatedly execute a block of code a specific number of times.

  • It consists of three parts: initialization, condition, and increment/decrement.

  • You can use the `break` statement to terminate the loop prematurely.

  • The `continue` statement allows you to skip the rest of the code inside the loop for the current iteration.

  • `for` loops can be nested to perform actions for every combination of two or more variables.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

What is JIT compiler? Is JavaScript compiled or interpreted or both?