Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>In short, you should go with whichever version is the easiest to read and maintain.</strong> </p> <p>In slightly older times, I know breaking out of a loop was considered to be a no-no (on par with a goto statement). Loops were supposed to break on the loop condition and nowhere else. Thus, the while-loop would have been the way to go. </p> <p>(This is probably a holdover from assembly, where loops are basically a block of code with a go-to-the-beginning-if-true jump statement at the end. Multiple conditional-jump statements in the block make it exceedingly hard to debug; thus they were to be avoided and combined into one at the end.)</p> <p>I feel this idea seems to be changing a bit today, especially with foreach loops and the managed world; it's really a matter of style now. Break-on-found for-loops have perhaps come to be acceptable to many, save some purists of course. Note that I would still avoid using break in a while-loop, however, as this can muddle the loop condition and make it confusing.</p> <p>If you'll allow me to use a foreach loop, I consider the code below to be a <em>lot</em> easier to read than its while-loop brother:</p> <pre><code>bool isBaxterInMilwaukee; foreach (var item in myArray) { if (item.name == "baxter" &amp;&amp; item.location == "milwaukee") { isBaxterInMilwaukee = true; barkTwice(); break; } } </code></pre> <p>However, as the logic grows in complexity, you may want to consider a prominent comment near the <code>break</code> statement lest it become buried and hard to find. </p> <hr> <p><strong>Arguably, this whole thing should be refactored into its own function</strong> which doesn't <code>break</code> on found, but actually <code>return</code>s the result (feel free to use the for-loop version instead):</p> <pre><code>bool isBaxterInMilwaukee(Array myArray) { foreach (var item in myArray) { if (item.name == "baxter" &amp;&amp; item.location == "milwaukee") { barkTwice(); return true; } } return false; } </code></pre> <p>As Esko Luontola pointed out, it would probably be best to move the call to <code>barkTwice()</code> outside of this function as the side-effect is not evident from the function's name, nor related to finding Baxter in every case. (Or add a boolean parameter <code>BarkTwiceIfFound</code> and change the line to read <code>if(BarkTwiceIfFound) barkTwice();</code> to make the side-effect clear.)</p> <hr> <p>For the record, you can also do the flag check in the for-loop without a break, but I feel this actually hurts readability because you don't expect an extra condition in a for-loop definition:</p> <pre><code>var i:int; var isBaxterInMilwaukee:Boolean; for (i = 0; !isBaxterInMilwaukee &amp;&amp; i &lt; arrayLen; i++) { if (myArray[i]["name"] == "baxter" &amp;&amp; myArray[i]["location"] == "milwaukee") { isBaxterInMilwaukee = true; barkTwice(); } } </code></pre> <p>You can also simulate auto-incrementing mechanics with a while-loop. I don't like this for a few reasons - you have to initialize <code>i</code> to be one less than your real starting value, and depending on how your compiler short-circuits the loop-condition logic, your value of <code>i</code> on exiting the loop may vary. Nevertheless, it is possible and for some people, this can improve readability:</p> <pre><code>var i:int = -1; var isBaxterInMilwaukee:Boolean; while (!isBaxterInMilwaukee &amp;&amp; ++i &lt; arrayLen) { if (myArray[i]["name"] == "baxter" &amp;&amp; myArray[i]["location"] == "milwaukee") { isBaxterInMilwaukee = true; barkTwice(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload