Break and continue

Two instructions that increase control over loops in Javascript. They are used to stop and continue with the next iteration of the loop respectively.

Javascript has different control structures to implement loops, such as FOR, WHILE and DO…WHILE, which we have already been able to explain in previous chapters of the . As we have been able to verify, with these loops we can cover a large number of needs, but perhaps over time you will find that you are missing some possibilities to control the repetitions of the loops.

Imagine for example that you are doing a very long loop to find something in hundreds or thousands of sites. But put yourself in the case that during the first iterations you find that value you were looking for. Then there would be no point in continuing with the rest of the loop to search for that item, since you had already found it. In these situations it is convenient for us to know for the loop to cancel the rest of the iterations. Obviously, this is just one example of how we might need to control the loop a bit more. In real life as a programmer you will find many other occasions when you will be interested in doing this or other things with them.

Thus, there are two instructions that can be used in the different control structures and mainly in loops, which will help you control two types of situations. They are the break and continue instructions:

  • break: It means to stop the execution of a loop and exit it.
  • continue: Used to stop the current iteration and return to the beginning of the loop to perform another iteration, if applicable.
See also  Cloud9 Online Development IDE

Break

A loop is stopped using the word break. Stopping a loop means getting out of it and leaving everything as it is to continue the flow of the program immediately after the loop.

for (i=0;i<10;i++){ document.write (i) write = prompt("tell me if I keep asking...", "yes") if (write == "no") break }

This example writes the numbers from 0 to 9 and at each iteration of the loop asks the user if they want to continue. If the user says anything it continues, except when he says “no”, a situation in which it exits the loop and leaves the account where it left off.

continue

It is used to return to the beginning of the loop at any time, without executing the lines below the word continue.

var i=0 while (i<7){ increment = prompt("Count is at " + i + ", tell me if increment", "yes") if (increment == "no") continue i++ }

This example would normally count up from i=0 to i=7, but every time the loop runs it asks the user if they want to increment the variable or not. If you enter “no” the continue statement is executed, which returns to the beginning of the loop without increasing the variable i by 1, since the statement below the continue would be ignored.

Additional example of the break statement

A more practical example of these instructions can be seen below. It is a FOR loop planned to reach 1,000 but we are going to break it when we reach 333.

for (i=0;i<=1000;i++){ document.write(i + "
“) if (i==333) break; }

Can .

With the description of the break and continue statements we have been able to cover everything there is to know about creating loops in Javascript. Now, in the next article we are still going to continue on the subject of control structures, because we want to offer a slightly more advanced example where .

Loading Facebook Comments ...
Loading Disqus Comments ...