Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 13
    Possibly, but despite the code samples the question being asked is IMO more conceptual, which is more in line with this area of SE. What is a pattern or anti-pattern is discussed here often, and that's the real question; is structuring a loop to run infinitely and then breaking out of it a bad thing? Commented Mar 29, 2012 at 18:48
  • 3
    The do ... until construct is also useful for these kinds of loops since they don't have to be "primed." Commented Mar 29, 2012 at 18:57
  • 2
    The loop-and-a-half case still lacks any really good answer. Commented Mar 29, 2012 at 23:25
  • 1
    "However, this duplicates a call to a method in code, violating DRY" I disagree with that assertion and it seems like a misunderstanding of the principle. Commented May 25, 2014 at 17:53
  • 2
    Apart from me preferring C-style for (;;) which explictely means "I have a loop, and I don't check in the loop statement", your first approach is absolutely right. You have a loop. There's some work to do before you can decide whether to exit. Then you exit if some conditions are met. Then you do the actual work, and start all over. That's absolutely fine, easy to understand, logical, non-redundant, and easy to get right. The second variant is just awful, while the third is merely pointless; turning to awful if the rest of the loop that goes into the if is very long. Commented Nov 11, 2015 at 20:15