Skip to content
the poor coder

you are working on javascript project.what you used to restart the innermost loop

Poor Man
Published
Reading time
2 min read

You are working on a JavaScript project. What do you use to restart the innermost loop?#

When working on a JavaScript project, there may be times when you want to restart the innermost loop. This can be accomplished using the continue statement.

The continue statement is used to skip one iteration of a loop and continue with the next iteration. In the case of a nested loop, the continue statement will only skip the current iteration of the innermost loop and continue with the next iteration of that same loop.

Example:#

Let's say we have a nested loop that prints out all possible combinations of two numbers from 1 to 5:

Text

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

The output of this code will be:

Text

1, 1
1, 2
1, 3
1, 4
1, 5
2, 1
2, 2
2, 3
2, 4
2, 5
3, 1
3, 2
3, 3
3, 4
3, 5
4, 1
4, 2
4, 3
4, 4
4, 5
5, 1
5, 2
5, 3
5, 4
5, 5

Now, let's say we want to skip all the iterations where the first number is less than or equal to the second number. We can use the continue statement to accomplish this:

Text

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

The output of this code will be:

Text

2, 1
3, 1
3, 2
4, 1
4, 2
4, 3
5, 1
5, 2
5, 3
5, 4

As you can see, the continue statement allowed us to skip all the iterations where the first number was less than or equal to the second number, and continue with the next iteration of the innermost loop.

In conclusion, the continue statement is a useful tool for controlling the flow of nested loops in JavaScript.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC