'break' & 'continue' statements

Introduction

In JavaScript, the `break` and `continue` statements are used to control the flow of loops. They are often used inside loops to change the normal execution behavior.

The `break` statement is used to exit a loop completely, while the `continue` statement is used to skip the current iteration of a loop and continue to the next iteration. These statements are particularly useful in situations where you need to terminate a loop prematurely or skip certain iterations based on certain conditions.

The `break` Statement

The `break` statement is used to exit a loop prematurely. When the `break` statement is encountered inside a loop, the loop immediately terminates and the program execution continues with the next statement after the loop.

Here's a basic example that uses the `break` statement in a while loop:

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

In this example, the loop will execute until `i` is equal to 5. When `i` becomes 5, the `break` statement is encountered, and the loop is terminated. Therefore, only the numbers 0, 1, 2, 3, and 4 will be logged to the console.

The `continue` Statement

The `continue` statement is used to skip the current iteration of a loop and proceed with the next iteration. When the `continue` statement is encountered inside a loop, the remaining code in the loop body for the current iteration is skipped, and the loop immediately jumps to the next iteration.

Here's an example that uses the `continue` statement in a for loop:

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

In this example, the loop will execute five times, but when `i` is equal to 2, the `continue` statement is encountered. As a result, the code inside the loop body for that iteration is skipped, and the loop proceeds to the next iteration. Therefore, the output will be 0, 1, 3, and 4.

Nesting Loops and Using `break` and `continue`

The `break` and `continue` statements can also be used with nested loops. When used within nested loops, the `break` statement terminates the innermost loop it is inside, while the `continue` statement skips the remaining code for the current iteration in the innermost loop.

Here's an example that demonstrates the use of `break` and `continue` statements in nested loops:

for (let i = 0; i < 3; i++) {
  console.log("Outer loop: " + i);
 
  for (let j = 0; j < 3; j++) {
    if (j === 1) {
      continue;
    }
 
    if (i === 2 && j === 2) {
      break;
    }
 
    console.log("Inner loop: " + j);
  }
}

In this example, there are two nested loops. The inner loop skips the iteration when `j` is equal to 1 using the `continue` statement. The outer loop terminates completely when `i` is equal to 2 and `j` is equal to 2 using the `break` statement. Therefore, the output will be:

Outer loop: 0
Inner loop: 0
Inner loop: 2
Outer loop: 1
Inner loop: 0
Inner loop: 2

Key Takeaways

  • The `break` statement is used to immediately terminate a loop and continue with the next statement after the loop.

  • The `continue` statement is used to skip the current iteration of a loop and proceed with the next iteration.

  • The `break` and `continue` statements can be used with nested loops to control the flow of execution.

  • The `break` statement terminates the innermost loop it is inside, while the `continue` statement only skips the remaining code for the current iteration in the innermost loop.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

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