Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Jumping out of a heavily-nested looping noy by using "goto" statement?

Jumping out of a heavily-nested looping noy by using "goto" statement?

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfwcfcomoop
22 Posts 14 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Link2600
    wrote on last edited by
    #1

    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".

    R M J B N 8 Replies Last reply
    0
    • L Link2600

      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".

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Alex Ngai wrote: goto statement is evil, It is not evil to use a goto statement when it transfers control to the end of the containing block. The C language even supplies you a couple of built-in constructs to make this easy to do - continue and break, the latter being nothing more than a continue followed by a loop terminator. If you can't get yourself to use a goto for fear that someone will throw things at you in a code review (because they "learned in school that a goto is bad, bad, very bad"), you could instead throw an exception when you want to break out of a nested block, and take appropriate action in the catch. Whatever you do, be sure to write a nice comment explaining what you've done. The person who ends up maintaining your code (usually yourself) will thank you for this while debugging at 3am on a winter's night. :) /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

      M 1 Reply Last reply
      0
      • L Link2600

        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".

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        Just use a goto. You could use a meaningless throw with a catch at the point you want to jump to, but why do that? The end result is the same, and even less clear because there's not really an exception happening. If someone bitches at you for the goto, ask them for better suggestions. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Come quietly or there will be... trouble.

        1 Reply Last reply
        0
        • L Link2600

          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".

          J Offline
          J Offline
          Johan Rosengren
          wrote on last edited by
          #4

          I would advice against using exceptions for application flow, I've seen this mismanaged far too many times. 'goto's are not evil, programmers are :-), but in addition to different ways of breaking, you might want to revise your heavily nested loop and see if you can simplify it.

          1 Reply Last reply
          0
          • L Link2600

            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".

            B Offline
            B Offline
            bikram singh
            wrote on last edited by
            #5

            Alex Ngai wrote: heavily-nested loop You should attempt to restructure your code. Heavily nested statements (more than 3 levels deep), are not good programming practice! So, I suggest, that while "goto" is fine to use in your case (remember, everything has its value), but I strongly suggest your restructure your loops! cheers! Bikram Singh

            K 1 Reply Last reply
            0
            • R Ravi Bhavnani

              Alex Ngai wrote: goto statement is evil, It is not evil to use a goto statement when it transfers control to the end of the containing block. The C language even supplies you a couple of built-in constructs to make this easy to do - continue and break, the latter being nothing more than a continue followed by a loop terminator. If you can't get yourself to use a goto for fear that someone will throw things at you in a code review (because they "learned in school that a goto is bad, bad, very bad"), you could instead throw an exception when you want to break out of a nested block, and take appropriate action in the catch. Whatever you do, be sure to write a nice comment explaining what you've done. The person who ends up maintaining your code (usually yourself) will thank you for this while debugging at 3am on a winter's night. :) /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              I recently saw a horrible code structure (thankfully that I didn't have to maintain) that looked like:

              while ( TRUE )
              {
              /* actions */

              if ( condition1 )
              {
              /* actions */
              break;
              }

              /* actions */

              if ( condition2 )
              {
              break;
              }

              /* actions */

              break;
              }

              Note the break at the bottom of the while block. That's right: the while block isn't actually a loop at all - it's simply a device to get break to skip to the bottom of the block. I'M NOT RECOMMENDING THIS. I think it's a horrible practice. It took me a good two hours to actually realise that it was, in fact, a goto. When Dijkstra originally wrote his 'goto considered harmful' paper, he was pointing out the benefits of block control structures for those programmers using languages without them, or die-hard programmers not using those structures in the new languages. Uncontrolled, poorly-designed use of gotos is simply confusing - it's hard for a maintainer to know what's going on - but so is the misuse of any control structure. I don't recommend misusing exceptions in the way you suggest, either. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^]. Stability. What an interesting concept. -- Chris Maunder

              P R 2 Replies Last reply
              0
              • L Link2600

                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".

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #7

                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.

                D 1 Reply Last reply
                0
                • M Mike Dimmick

                  I recently saw a horrible code structure (thankfully that I didn't have to maintain) that looked like:

                  while ( TRUE )
                  {
                  /* actions */

                  if ( condition1 )
                  {
                  /* actions */
                  break;
                  }

                  /* actions */

                  if ( condition2 )
                  {
                  break;
                  }

                  /* actions */

                  break;
                  }

                  Note the break at the bottom of the while block. That's right: the while block isn't actually a loop at all - it's simply a device to get break to skip to the bottom of the block. I'M NOT RECOMMENDING THIS. I think it's a horrible practice. It took me a good two hours to actually realise that it was, in fact, a goto. When Dijkstra originally wrote his 'goto considered harmful' paper, he was pointing out the benefits of block control structures for those programmers using languages without them, or die-hard programmers not using those structures in the new languages. Uncontrolled, poorly-designed use of gotos is simply confusing - it's hard for a maintainer to know what's going on - but so is the misuse of any control structure. I don't recommend misusing exceptions in the way you suggest, either. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^]. Stability. What an interesting concept. -- Chris Maunder

                  P Offline
                  P Offline
                  peterchen
                  wrote on last edited by
                  #8

                  how about

                  do { // while 0, block to exit with break
                  // same pain as above
                  } while(0);


                  we are here to help each other get through this thing, whatever it is Vonnegut jr.
                  sighist || Agile Programming | doxygen

                  1 Reply Last reply
                  0
                  • B bikram singh

                    Alex Ngai wrote: heavily-nested loop You should attempt to restructure your code. Heavily nested statements (more than 3 levels deep), are not good programming practice! So, I suggest, that while "goto" is fine to use in your case (remember, everything has its value), but I strongly suggest your restructure your loops! cheers! Bikram Singh

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #9

                    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

                    B 1 Reply Last reply
                    0
                    • M Mike Dimmick

                      I recently saw a horrible code structure (thankfully that I didn't have to maintain) that looked like:

                      while ( TRUE )
                      {
                      /* actions */

                      if ( condition1 )
                      {
                      /* actions */
                      break;
                      }

                      /* actions */

                      if ( condition2 )
                      {
                      break;
                      }

                      /* actions */

                      break;
                      }

                      Note the break at the bottom of the while block. That's right: the while block isn't actually a loop at all - it's simply a device to get break to skip to the bottom of the block. I'M NOT RECOMMENDING THIS. I think it's a horrible practice. It took me a good two hours to actually realise that it was, in fact, a goto. When Dijkstra originally wrote his 'goto considered harmful' paper, he was pointing out the benefits of block control structures for those programmers using languages without them, or die-hard programmers not using those structures in the new languages. Uncontrolled, poorly-designed use of gotos is simply confusing - it's hard for a maintainer to know what's going on - but so is the misuse of any control structure. I don't recommend misusing exceptions in the way you suggest, either. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^]. Stability. What an interesting concept. -- Chris Maunder

                      R Offline
                      R Offline
                      Ravi Bhavnani
                      wrote on last edited by
                      #10

                      Mike Dimmick wrote: horrible code structure Yikes! Three nested if-elses would have sufficed, since that it in fact the programmer's intent. Mike Dimmick wrote: I don't recommend misusing exceptions in the way you suggest, either. Neither do I, as I'm sure you'd surmise if you re-read the "if" preceding the "suggestion". :) /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

                      1 Reply Last reply
                      0
                      • L Link2600

                        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".

                        P Offline
                        P Offline
                        palbano
                        wrote on last edited by
                        #11

                        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

                        L 1 Reply Last reply
                        0
                        • P palbano

                          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

                          L Offline
                          L Offline
                          Link2600
                          wrote on last edited by
                          #12

                          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".

                          P B 2 Replies Last reply
                          0
                          • L Link2600

                            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".

                            P Offline
                            P Offline
                            palbano
                            wrote on last edited by
                            #13

                            Alex Ngai wrote: Unfortunately, at least 3-nested loop is a common practice: X| Smoking crack is a common practice, but what does that have to do with Object Oriented Design? :-D

                            "No matter where you go, there your are." - Buckaroo Banzai

                            -pete

                            1 Reply Last reply
                            0
                            • L Link2600

                              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".

                              K Offline
                              K Offline
                              KarstenK
                              wrote on last edited by
                              #14

                              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)

                              L 1 Reply Last reply
                              0
                              • K KarstenK

                                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)

                                L Offline
                                L Offline
                                Link2600
                                wrote on last edited by
                                #15

                                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".

                                1 Reply Last reply
                                0
                                • N Nemanja Trifunovic

                                  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.

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  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

                                  H 1 Reply Last reply
                                  0
                                  • D David Crow

                                    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

                                    H Offline
                                    H Offline
                                    Henry miller
                                    wrote on last edited by
                                    #17

                                    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.

                                    1 Reply Last reply
                                    0
                                    • L Link2600

                                      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".

                                      B Offline
                                      B Offline
                                      bikram singh
                                      wrote on last edited by
                                      #18

                                      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

                                      1 Reply Last reply
                                      0
                                      • K Kevin McFarlane

                                        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

                                        B Offline
                                        B Offline
                                        bikram singh
                                        wrote on last edited by
                                        #19

                                        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

                                        K 1 Reply Last reply
                                        0
                                        • L Link2600

                                          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".

                                          C Offline
                                          C Offline
                                          Chris Hills
                                          wrote on last edited by
                                          #20

                                          Ravi Bhavnani wrote: If you can't get yourself to use a goto ... you could instead throw an exception I disagree. If goto is evil then throwing an exception just to avoid using a goto is even worse! Which one is clearer to the person maintaining the code? Which one are you more likely to get wrong? Sometimes a goto is the right way to do something so long as it's well commented and isn't abused.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups