Control the loop in JavaScript
IT Tricks

Control the loop in JavaScript


JavaScript provides high control to handle loops and switch statements. There may be a situation when you need to exit the loop without having to go to its endpoint. There may also be a situation when you want to jump over part of the code block and start the next loop.

To handle the above scenarios, JavaScript provides commands break and tiếp tục. These commands are used to immediately exit any loop or to start the next loop of any corresponding loop.

Break statement

Comeinand break, was briefly introduced with the switch command, which is used to early exit out of a loop, escape the braces area.

Implementation diagram

The execution diagram of a break statement is as follows:

Control loop diagram in JavaScript

For example

The following example illustrates the use of the command break with while loop. Notice how the loop exits early as x approaches 5 and go to the command document.write (…) right below the closing brace.

   type="text/javascript">   

Set the variable to different value and then try ...

Result

Entering the loop
2
3
4
5
Exiting the loop!
Set the variable to different value and then try...

We saw the usage of the command break inside a command switch.

Continue statement

The continue statement tells the interpreter to immediately start the next iteration of the loop and jump over the remaining code blocks. When a continue statement is encountered, the program thread moves immediately to the test expression and if the condition remains true, then it begins the next iteration, otherwise the control exits the loop.

For example

This example illustrates the use of the continue statement with a while loop. Notice how the continue statement is used to jump over print when the index is kept in value x proceed to 5.

   type="text/javascript">   

Set the variable to different value and then try ...

Result

Entering the loop
2
3
4
6
7
8
9
10
Exiting the loop!

Use Label to control the Flow (Flow)

Starting with JavaScript 1.2, a Label (trademark) can be used with break and tiếp tục for more precise flow control. One label is simply an identifier followed by a colon (:) that is applied to a command or block of code. We will see two different examples to understand how to use labels with break and continue.

Note – Interrupt lines are not allowed between commands tiếp tục or break and name label its. Also, there shouldn’t be any other commands between a label name and a link loop.

Try the following two examples to gain a deeper understanding of Label.

Example 1

This example shows how to execute Label with a break statement:

   type="text/javascript">    

Result

Entering the loop!
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 2
Outerloop: 3
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 4
Exiting the loop!

Example 2

This example shows how to implement Label with a continue statement:

   type="text/javascript">    

Result

Entering the loop!
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 2
Innerloop: 0
Innerloop: 1
Innerloop: 2
Exiting the loop!

Follow Tutorialspoint

Previous lesson: The for … in loop

Next article: Functions in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *