Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Why isnt there a numbered, multi-level break statement to break out
Why isn't there a numbered, multi-level break statement to break out of several loops at once? What am I supposed to use instead, a goto?
✍: Guest
A:First, remember why it is that break and continue exist at all--they are, in effect, ``structured gotos'' used in preference to goto (and accepted as alternatives by most of those who shun goto) because they are clean and structured and pretty much restricted to a common, idiomatic usages. A hypothetical multi-level break, on the other hand, would rapidly lose the inherent cleanliness of the single break--programmers and readers of code would have to carefully count nesting levels to figure out what a given break did, and the insertion of a new intermediately-nested loop could, er, break things badly. (By this analysis, a numbered break statement can be even more confusing and error-prone than a goto/label pair.)
The right way to break out of several loops at once (which C also does not have) involves a syntax which allows the naming of loops, so that a break statement can specify the name of the loop to be broken out of.
If you do have to break out of more than one loop at once (or break out of a loop from inside a switch, where break would merely end a case label) yes, go ahead and use a goto. (But when you find the need for a multi-level break, it's often a sign that the loop should be broken out to its own function, at which point you can achieve roughly the same effect as that multi-level break by using a premature return.)
2015-01-30, 1594👍, 0💬
Popular Posts:
How was XML handled during COM times? During COM it was done by using MSXML 4.0. So old languages li...
.NET INTERVIEW QUESTIONS - How can you avoid deadlock in threading? A good and careful planning can ...
What’s the difference between Unit testing, Assembly testing and Regression testing? Unit testing is...
what are the advantages of hosting WCF Services in IIS as compared to self hosting? There are two ma...
What Is C Language? The C programming language is a standardized programming language developed in t...