Continue and break statements are essentials to almost any programming language. Learning how to use them and what they do in a simple language like javascript can really have an impact on your clarity of interpreting and writing code.

This code is ridiculously simple, but quite a delightful usage of the continue statement in javascript.  It also annoyingly bombards the viewer with 4 alert boxes, but the blatant bombardment makes the effect of the continue statement less ambiguous, kind of an obvious Alert, Alert, Alert, “Oh that’s what continue does!”.
var a = 0;
for (i=0;i{
if (i==5 || i==10 || i==15 || i==20)
{
alert( i + " is a...MULTIPLE OF FIVE!");
document.write("
");
continue;
}
document.write("The number is " + i);
document.write("
");

If a break statement were substituted in the above code, the output would be:

This is number 1
Thus is number 2
This is number 3
This is number 4
5 is a…MULTIPLE OF FIVE! //alert box

and then terminate

Out of all the languages, learning about continue and break statements in javascript and python is probably best because there’s less chance your usage of them will be clouded by the complex nuances of heavier languages. Libre Office, for example, (a full fledged presentation, spreadsheet, wordpressing, database, drawing, and more opensource word-processor suite) was coded in C++ and Java. Of course, there’s continue and break statements in those languages, but they’re full-fledged languages and for just learning what a continue or break statement does, languages that require less architecture to implement a few commands (like python or javascript) are great.

From slashdot, ErichTheRed writes:

“This Computerworld piece actually got me thinking — it basically says that there are few good ‘starter languages’ to get students interested in programming. I remember hacking away at BASIC incessantly when I was a kid, and it taught me a lot about logic and computers in general. Has the level of abstraction in computer systems reached a point where beginners can’t just code something quick without a huge amount of back-story? I find this to be the case now; scripting languages are good, but limited in what you can do… and GUI creation requires students to be familiar with a lot of concepts (event handling, etc.) that aren’t intuitive for beginners. What would you show a beginner first — JavaScript? Python? How do you get the instant gratification we oldies got when sitting down in front of the early-80s home computers?”

That’s 100% spot-on and precisely what to what I’ve been alluding. However, that said, I think javascript and especially python deserve more credit. They fantastic starter languages, but they’re extensible. Addon libraries and modules like pygame for example make these languages very complete and much more capable than something like BASIC, and yet they have the awesome “great introduction, no need to get overwhelmed” feel as what EnrichTheRed refers to as starter languages. But Python is infinitely much more than a BASIC 2.0. It’s a full-fledged language, intuitively written so that it’s elegant, a great introduction for beginners, but decidedly complete and thorough enough like a full-fledged language in some ways for more advanced coders/programmers.

So What Exactly Does Break and Continue “Do”?

So just to clarify, what does the continue statement actually “do”?  Well, both continue and break say to the current conditional (if, if-else) or iterative (for, while, do-while) loop

“Don’t execute subsequent code in the function!”

Both the continue and break statements do that, but the difference is that continue simple reevaluates the function again, in a sense merely “skipping” the subsequent statements after  that one instance that brought continue to be executed.  The break statement, on the other hand, says “Don’t execute subsequent code in the function!” and then breaks out of the rest of the loop, in a sense aborting the rest of the iterative or conditional loop.

Thus, I like to think of the break statement as an “abort!” and the continue as “hold up, I’ll take that or I’ll adjust that, now back to what you were doing!” effect.  You can connote whatever you want with them, but continue and break are extremely potent statements to make your functions, loops, and conditionals behave in advanced ways, accomplishing what you want!

Other Definitions

Some other resources (defining the exact same thing we have detailed above but just using slightly different wording, because people learn differently, the altered wording may help or hurt your comprehension).

From IBM’s site (just one of the many random resources detailing continue statements):

“CONTINUE STATEMENT

continue statement ends the current iteration of a loop. Program control is passed from the continue statement to the end of the loop body.

continue statement has the form:

>>-continue--;-------------------------------------------------><

continue statement can only appear within the body of an iterative statement.

The continue statement ends the processing of the action part of an iterative (dofor, or while) statement and moves control to the loop continuation portion of the statement. For example, if the iterative statement is a for statement, control moves to the third expression in the condition part of the statement, then to the second expression (the test) in the condition part of the statement.

Within nested statements, the continue statement ends only the current iteration of the dofor, or while statement immediately enclosing it.”

This is all basically what we just said.  The continue statement’s “processing” and brings the focus back to the loop-control (in “for” this would be the increment, in a “while” this would be the conditional evaluation and the like!)

Advertisements

About V.P.

meh meh.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s