We can come out of any loop's execution, whenever required, by using the
break statement and we can also skip the current iteration and start next iteration by using the
Let us recall the
break statement once again, the
break statement lets us break out of the loop execution or any
switch statement and move on to execute the code after the end of the loop or switch.
Here is a simple example,
In the above example, when the value of the variable
count becomes divisible by 5, we break out of the loop, and hence the loop execution stops. Try removing the
break statement and you will see the loop will get executed 10 times.
continue statement can be used to skip the current loop iteration and jump to the next iteration if the loop condition is satisfied. This is used in case, for some specific condition in the loop execution you want to perform no action and directly jump to next iteration then the
continue statement can be used.
Let's take an example,
In the code example above, whenever the value of the variable
count is divisible by 2, we do nothing and call the
continue statement to directly jump to the next iteration of the loop. Hence you can see that the
document.write code is executed for only those iterations where the value of the
count variable was not divisible by 2, which means for odd values only.
continue statements can only be used to break or skip iteration of a code block in a loop or switch case, using a label with
continue statement lets you control the execution of any code block.
:) and then your code.
continue followed by the label_name.
Let's take an example to understand this.
As you can see in the example above, we have specified a label with name EXECUTE and then used it along with the
break statement to specify which code block to break out of.
In this tutorial, we learned about the
continue statements which are very important statements when it comes to controlling the flow of execution of the code within a loop or outside a loop.