Obj-C: breaking out of loops and conditionals

Discussion in 'Mac Programming' started by GregX999, May 9, 2009.

  1. GregX999 macrumors newbie

    GregX999

    Joined:
    Apr 5, 2009
    #1
    I have a pretty basic (I think) question. If I have a set of nested loops and/or conditionals, for example like this:

    Code:
    for (Hex *h in potentialNextHexes) {
    	hexIntersect = [self hex:h IntersectsLineFrom:hOrigin to:hTarget];
    	if ([self distanceBetweenHex:h andHex:hTarget] <= distance && hexIntersect > HEX_WAS_MISSED) {
    		if (hexIntersect == HEX_WAS_CROSSED) {
    			*next1 = h;
    			STOP THE LOOP!!!
    		}
    		if (*next1 == nil) *next1 = h;
    		else *next2 = h;
    	}
    }
    What command can I replace "STOP THE LOOP!!!" with that will break out of the two if statements and the for loop? (I don't want the for-loop to continue no matter how many "potentialHexes" may be left to still iterate through.)

    Thanks,
    Greg
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    Code:
    break;
    
    Like any other C based language ;).
     
  3. GregX999 thread starter macrumors newbie

    GregX999

    Joined:
    Apr 5, 2009
    #3
    Oh, I thought that would just break out of the if block. I see now that it only breaks out of loops.

    Obj-C is my first encounter with a C based language. :)

    Thanks!
    Greg
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    Often time, when you want to get out of a nested loop you are done with whatever method or fuction you were trying to do (you can probably always make this the case by creating a function/method to do whatever the nested loop is doing).

    Hence, you could just use
    return; // and return whatever it is you want if you need to.
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Another option is to use goto to break out of deep nested loops. One of the few situations where goto seems to be accepted.
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    I have heard goto has become highly frowned upon.
     
  7. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    For its worth C based languages do include Java, Javascript and C# as well as C, C++ and objective C ;).

    By this I mean that if you know how to do something in Java, C# (especially) or Javascript it'll probably be the same in C.
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    Not when used in the way I suggested.

    Goto is not bad in itself, it is just bad when used for silly reasons.
     
  9. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #9
    Though goto isn't always used in a bad way, it might be that there is always a better alternative. If you are considering goto first consider other options.

    jared_kipe's refactor is a good, general purpose alternative, for example, because when goto is used to implement flow-control operations it should never jump past the boundary of a single logical operation at any particular level -- actually a goto should only jump to the boundary of a logical operation. In the refactor that jared_kipe suggests, the boundaries of the logical operation are defined by the function, which is nice and good for a bunch of reasons.

    goto is also legitimately used in certain error handling situations. But when this is done in a robust, organized and consistent way it starts to look a lot like exception handling. Why not simply use an exception handling mechanism? (You can very likely find an existing one for your environment so you won't need to implement your own).
     
  10. Ti_Poussin macrumors regular

    Ti_Poussin

    Joined:
    May 6, 2005
    #10
    using GOTO!!! a raptor will come to eat you if you do that (XKCD joke here)!
    GOTO is bad programming, you should not use that, except in really rare case, and this is not one.

    The function and break are the good solution here. Even a check on a boolean condition could be better then goto but still a bad approach.

    Edit: http://xkcd.com/292/
    lol
     
  11. sushi Moderator emeritus

    sushi

    Joined:
    Jul 19, 2002
    Location:
    キャンプスワ&#
    #11
    ^ Agree.

    With high level languages, only use a Goto statement as the last resort.
     
  12. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #12
    The only reason using goto statements is considered bad programming is because it can lead to spagetti code. If all you are using it for is jumping out of nested loops then that is not a danger.

    It often provides the easiest, quickest and arguably fastest implementation. I certainly agree that it is bad in the majoity of cases but this is not one.

    Still I guess it comes down to opinion. I've certainly seen goto statements used a lot in large development efforts (the Linux kernel for instance), so it seems that refusing to use it at all is somewhat limiting.
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    And a thorough consideration of what "last resort" means. I'll refactor a whole subroutine if it will save using a goto. This may seem like a waste, but when a bug or need for enhancement crops up in that area later, there's always a net savings.

    I ported (from HPUX on PA-RISC to Linux on x86) and now maintain ~25 year old fortran (a lot using pre-77 syntax). Cycle and exit are now available, allowing for reasonable loop execution control, but all of the old code has dozens of labels per file, with gotos at every turn. Following flow control is nearly impossible. This is an extreme case, but just one to avoid a refactor, as Ti_Poissin pointed out, uniformly leads to a raptor attack.

    -Lee
     
  14. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #14
    A project I'm working on uses goto all the time, and the main dev still uses it in new code, and uses it improperly. Mainly problems with goto-ing a label and using a pointer that may not have been assigned. Fortunately GCC catches those problems.
     
  15. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    Hopefully the folk writing that code know better, and have truly weighed the cost in readability with the performance of doing, say n-2 (where n is nesting depth) conditional branches to test a "done" variable at each level and break, etc. If you're writing code whose speed will truly suffer performance problems in this case, and there is no way around it, it would be fool-hardy to ignore the possibility of a goto. It should pain you, and you should have your raptor-gun ready, but it may be a compromise that has to be made. I have never run into a situation where a goto would need to be considered for performance reasons, but i'm sure the need does arise.

    iSee pointed out exception handling, and for performance reasons (at least as I understand it) C++ has been rejected for use in the Linux kernel, which would be the shortest path to exception handling.

    We're at well over a dozen posts, we've sort of beat this to death. The OP is new to programming, at least C-style syntax, so they're probably unlikely to be running into any situation where a goto is truly the only option. Imploring them against using it and instilling the fear of God/Raptor in them at this point is probably in everyone's best interest, in case the OP is going to post code snippets that we want to read here later, etc.

    -Lee
     

Share This Page