How to terminate or stop from a loop for only one iteration in javascript?
Explanation
Continue statement is used to stop or terminate one iteration of the loop in execution.
It is used in "for, while, do-while" looping statements.
Continue statement unlike break statement does not completely terminate the loop.
It stops processing for only one iteration and brings the control back to the beginning of
the loop.
For an example, we will try to stop processing inside a for loop when i is 2.
Example Code:
<script language="javascript">
for(var i=0; i<5; i++)
{
if(i == 2)
continue;
document.write("i is - "+i);
}
</script>
Result:
The example worked as follows,
a) This for loop has five iterations from i=0 to i=4.
b) It executes document.write at every iteration.
c) when i is 2 the condition above document.write becomes true and so it is called.
d) continue statement terminates the looping sequence and brings the control to
the beginning of loop, starting the next iteration.
e) Thus the print was not done for i as 2.
A Note
Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script.
Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.