|
|
JavaScript ‘Loop control’ enables us to have control over the loops. You may encounter a situation where you want to exit the loop without reaching its end. In other words, you may require to skip a block of statements and start the next iteration of loop. There may be a situation when you need to come out of a loop without reaching its bottom
Using break statement, we can break the loop and continue the execution of rest of code that follows after the loop. We have already discussed the use of break statement in Switch. We can stop the particular case by placing the break statement at the end of the case.
The break statement stops the execution of loop where as the continue statement starts the next iteration of the loop leaving the remaining bock of code. When a continue statement is found, the condition is re-evaluated. If it returns true then it starts the next iteration, if it returns false the control exits the loop.
Example
The below example explains the usage of a continue statement with a while loop. Observe how the continue statement skips the printing of the variable x when the index reaches 5.
The label statement can be used with continue and break statements to control the flow more accurately. A label is simply an identifier
followed by a colon (:) that is applicable to group of statements. Let us see how to use labels with break and continue statements with the help of below examples:
Note –Make sure we should not place any other statement between a label name and corresponding loop.
Example 2
|
|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.