Javascript: More Loops and Events - And So it Continues
(Page 3 of 4 )
The Continue statement is similar to the Break statement, only instead of breaking completely out of the loop, it simply breaks and continues to the next value.
Behold!
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=5;i++)
{
if (i==3){continue}
document.write("I drank this many beers: " + i)
document.write("<br />")
}
</script>
</body>
</html>
This will result in the print out:
I drank this many beers: 0
I drank this many beers: 1
I drank this many beers: 2
I drank this many beers: 4
I drank this many beers: 5
As you can see, we skipped the number 3. Hey, you drink 5 beers and try counting! It is a very similar code to the Break, however we choose to not break completely out of the loop.
If you want to use this to make a funnier (or sadder if you really count like this) program you could do this:
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=4;i++)
{
if (i==3){continue}
document.write("Look at me I can count!... " + i)
document.write("<br />")
}
var i=0
for (i=6;i<=7;i++)
{
if (i==7){continue}
document.write("Look at me I can count!... " + i)
document.write("<br />")
}
var i=0
for (i=8;i<=10;i++)
{
if (i==9){continue}
document.write("Look at me I can count!... " + i)
document.write("<br />")
}
</script>
<p><b>I R SMARTED!</b></p>
</body>
</html>
Here I have used several Loops and Continues to achieve this effect:
Look at me I can count!... 0
Look at me I can count!... 1
Look at me I can count!... 2
Look at me I can count!... 4
Look at me I can count!... 6
Look at me I can count!... 8
Look at me I can count!... 10
Next: Finishing Loops >>
More JavaScript Articles
More By James Payne