Jumping out of a heavily-nested looping noy by using "goto" statement?
-
Is there a way to jump of out a heavily-nested loop not by using goto statement? Since most people would agree that goto statement is evil, and it will easily destory a program's structure, so, I want to stay away from it. However, to my surprise, there is really no easy and effective way to jump out of a heavily-nested loop not by using it. Well, I thought I should consult those experts on codeproject.com, so I could learn from them -- you. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
Alex Ngai wrote: and it will easily destory a program's structure, and Alex Ngai wrote: a heavily-nested loop So you want to protect the structure of your heavily nested loop code? Isn't that a bit of an oxymoron? If you are actually concerned with program structure you might consider refactoring the code into a modern object oriented design that utilizes known patterns and techniques to solve simple structure problems like nested loops and 500 line if/elseif/switch statements. But, that's just my opinion... I could be wrong.
Hate is not a family value
-pete
-
Alex Ngai wrote: and it will easily destory a program's structure, and Alex Ngai wrote: a heavily-nested loop So you want to protect the structure of your heavily nested loop code? Isn't that a bit of an oxymoron? If you are actually concerned with program structure you might consider refactoring the code into a modern object oriented design that utilizes known patterns and techniques to solve simple structure problems like nested loops and 500 line if/elseif/switch statements. But, that's just my opinion... I could be wrong.
Hate is not a family value
-pete
Thanks for all you guys's replies, Unfortunately, at least 3-nested loop is a common practice:
while( ) { for( ; ; ){ for( ; ; ){ } } }
Java does not have goto statement at all, but in order to jump out of a several-nested loop, Java has a smart extended break statement, it works like this:first:{ second:{ third:{ System.out.println("Before break."); if(condition) break second; System.out.println("This won't execute."); } } System.out.println("after second block."); }
As we see, it does the job of breaking out of a loop, and it won't have the problem of goto statment which will easily destroy a program's structure. Jave really is a great language, if only it was a system language like C/C++. ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented". -
Thanks for all you guys's replies, Unfortunately, at least 3-nested loop is a common practice:
while( ) { for( ; ; ){ for( ; ; ){ } } }
Java does not have goto statement at all, but in order to jump out of a several-nested loop, Java has a smart extended break statement, it works like this:first:{ second:{ third:{ System.out.println("Before break."); if(condition) break second; System.out.println("This won't execute."); } } System.out.println("after second block."); }
As we see, it does the job of breaking out of a loop, and it won't have the problem of goto statment which will easily destroy a program's structure. Jave really is a great language, if only it was a system language like C/C++. ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented". -
Is there a way to jump of out a heavily-nested loop not by using goto statement? Since most people would agree that goto statement is evil, and it will easily destory a program's structure, so, I want to stay away from it. However, to my surprise, there is really no easy and effective way to jump out of a heavily-nested loop not by using it. Well, I thought I should consult those experts on codeproject.com, so I could learn from them -- you. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
At first: my expirience is, if it is going to get too complex, there ist some going wrong. Try to make subfunctions.:suss: A good way to leave loops is to leave the whole funktion with "return" + exitcode. Another option ist to use a central boolean which in every loop is checked.:~ Try this @ home. (B&B)
-
At first: my expirience is, if it is going to get too complex, there ist some going wrong. Try to make subfunctions.:suss: A good way to leave loops is to leave the whole funktion with "return" + exitcode. Another option ist to use a central boolean which in every loop is checked.:~ Try this @ home. (B&B)
Well, the return statement only return back to the function calling point, that's breaking out of a function, not breaking out of a nested-loop. The second option seems interesting, would you explain it a little more? I'm interested. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
-
Just use
goto
and if some dumbass starts asking questions, just quote Stroustrup (TC++PL 3rd ed).
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Nemanja Trifunovic wrote: just quote Stroustrup (TC++PL 3rd ed). For those of us that do not have this book, would you please provide the relevant quote?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Nemanja Trifunovic wrote: just quote Stroustrup (TC++PL 3rd ed). For those of us that do not have this book, would you please provide the relevant quote?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
If you don't have it, you really should... Still there are those who can't afford it (yet...). This is from the special edition, which should be identical to the 3rd edition except most of the errata is corrected. (I used snip where I cut some text that is interesting, but doesn't apply here) I copied this by hand, so mistakes are mine. Page 137: The goto has few uses in general high-level programing, (snip) The goto can also be important in the rare save in which optimal efficiency is essential, (snip) (snip a couple paragraphs) One of the few sensible uses of goto in ordinary code is to break out from a nested loop or switch statement. There you have it: Most of the time goto should not be used. However there are exceptions to that rule, where goto makes the code more readable, or where there is critical speed problem that can be corrected by goto, use it. Remember when applying the latter that the premature application of optimization is evil! Only do so when you can't get a better algorithm or CPU, and a profiler reveales that this area is a problem. Goto will only at best save a few cycles from your loop, so it rarely is enough of a solution to help speed problems. Sometimes embedded systems run into those cases where it matters.
-
Thanks for all you guys's replies, Unfortunately, at least 3-nested loop is a common practice:
while( ) { for( ; ; ){ for( ; ; ){ } } }
Java does not have goto statement at all, but in order to jump out of a several-nested loop, Java has a smart extended break statement, it works like this:first:{ second:{ third:{ System.out.println("Before break."); if(condition) break second; System.out.println("This won't execute."); } } System.out.println("after second block."); }
As we see, it does the job of breaking out of a loop, and it won't have the problem of goto statment which will easily destroy a program's structure. Jave really is a great language, if only it was a system language like C/C++. ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".Seems to me, Java's "smart extended break statement" is nothing but a goto in disguise! A disguised goto is worse than if it was naked! :D Bikram Singh
-
bikram singh wrote: Heavily nested statements (more than 3 levels deep), are not good programming practice! I agree. But they're also (unfortunately) very common. Kevin
You're right there. But as KarstenK said, deep nesting is an indication that the code is too complex. There are very few loops out there that cant be broken down... sure you may need to use functions to accomplish it sometimes, but the cleaner code is well worth that effort. Bikram Singh
-
Is there a way to jump of out a heavily-nested loop not by using goto statement? Since most people would agree that goto statement is evil, and it will easily destory a program's structure, so, I want to stay away from it. However, to my surprise, there is really no easy and effective way to jump out of a heavily-nested loop not by using it. Well, I thought I should consult those experts on codeproject.com, so I could learn from them -- you. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
Ravi Bhavnani wrote: If you can't get yourself to use a
goto
... you could insteadthrow
an exception I disagree. Ifgoto
is evil thenthrow
ing an exception just to avoid using agoto
is even worse! Which one is clearer to the person maintaining the code? Which one are you more likely to get wrong? Sometimes agoto
is the right way to do something so long as it's well commented and isn't abused. -
You're right there. But as KarstenK said, deep nesting is an indication that the code is too complex. There are very few loops out there that cant be broken down... sure you may need to use functions to accomplish it sometimes, but the cleaner code is well worth that effort. Bikram Singh
I wasn't implying that I agreed with deeply nested conditionals and loops! Quite the opposite. Just observing that they're very common. This is another way of saying that there are lots of poor programmers out there. Kevin
-
I wasn't implying that I agreed with deeply nested conditionals and loops! Quite the opposite. Just observing that they're very common. This is another way of saying that there are lots of poor programmers out there. Kevin
I know ! The implication that i implicated was that the code was too complex, which implied that the implication i implicated was correct. I didnt imply that i was implicating you for your note, but just that i didnt really mean to implicate what you thought i was implicating. :) Bikram Singh