|
-
February 18th, 2006, 11:36 AM
#14
Re: Is using goto a bad practice
 Originally Posted by souldog
here is a very readable way to breakout of nested loops.
Code:
for(size_t i = 1, break_loops = 0; i < 10 && !break_loops; i++)
{
for(size_t j = 0; j < 10 && !break_loops; j++)
{
if(WHAT_EVER)
break_loops = 1;
}
}
If you'd have a little more code in your loop (like in some real life example):
Code:
for(size_t i = 1, break_loops = 0; i < 10 && !break_loops; i++)
{
for(size_t j = 0; j < 10 && !break_loops; j++)
{
if(WHAT_EVER)
break_loops = 1;
}
// MORE CODE HERE
}
the part "// MORE CODE HERE" will still be executed, unless, of course, you add multiple tests for "break_loops", wich doesn't help the things. Think about more than two nested loops. If the code is trivial - almost any solution will be readable.
About SuperKoko's suggestion of multiple return statements - I believe it isn't much better than goto's, and sometimes - much worse.
P.S. When we are done with goto, lets talk about use of tabs vs. spaces for indentation.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|