Loop control in JavaScript

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

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

To handle the above situations, JavaScript provides break and continue commands. These commands are used to immediately exit any loop or to start the next loop of any corresponding loop.

Break command

The break command, introduced briefly with the switch command, is used to exit soon from a loop, leaving the brace area.

Implementation diagram

Implementation diagram of a break statement is as follows:

Loop control in JavaScript Picture 1

For example

The following example illustrates the use of the break statement with a while loop . Notice how the loop exits early when x proceeds to 5 and goes to the document.write command (.) right below the closed parenthesis.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result

 

 Entering the loop 
2
3
4
5
Exiting the loop!
Đặt biến vào giá trị khác nhau và thử thử .

We have seen the usage of the break statement within a switch statement .

Continue statement

The continue statement tells the interpreter to immediately start the next iteration of the loop and jump over the remaining code block. When a continue statement is encountered, the program flow moves immediately to the test expression and if the condition is true, then it starts 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 the print when the index is kept in the x value 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
ten
Exiting the loop!

Use Label to control Flow (Flow)

Starting from JavaScript 1.2, a Label can be used with break and continue to control the flow more accurately. A label is simply an identifier followed by a colon (:) that applies 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 the continue or break statement and its label name. Also, there should not be any other commands between a label name and a link loop.

Try these two examples to get a deeper understanding of Label.

Example 1

This example shows how to perform 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 perform 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!

 

According to Tutorialspoint

Previous article: For . loop in JavaScript

Next article: Function in JavaScript

4 ★ | 1 Vote | 👨 249 Views
« PREV POST
NEXT POST »