title: Is there a "smart" way to break out of nested loops?
body: <p>(Right now I'm mostly using C#. Ideas in other languages are welcome, but please translate them to C# if you can, and be explicit.)</p>
  
  <p>Something I come across time and time again is a nested loop, searching through some 2D array to find an element (typically some object), which then has to be manipulated. So of course, once you find that object, you should break out of both loops so you don't needlessly continue the search for something you already found (especially in nested loops which can traversing exponentially huge arrays).</p>
  
  <p>The following code is currently my preferred way of doing it:</p>
  
  <pre><code>Obj O = null;
  bool KeepLooping = true;
  
  for (int j = 0; j &lt; height &amp;&amp; KeepLooping; j++)
  {
      for (int i = 0; i &lt; width; i++)
      {
          if (ObjArray[i, j] != null &amp;&amp; ObjArray[i, j].property == search_value)
          {
              O = ObjArray[i, j]; // you found it, now remember it
              KeepLooping = false; // clear the flag so the outer loop will break too
              break;
          }
      }
  }
  </code></pre>
  
  <p>Thanks to Erik Funkenbusch, it becomes much more elegant if we do this:</p>
  
  <pre><code>Obj O = null;
  for (int j = 0; j &lt; height &amp;&amp; O == null; j++) // much, much better idea to check O for null in the outer loop
  {
      for (int i = 0; i &lt; width; i++)
      {
          if (ObjArray[i, j] != null &amp;&amp; ObjArray[i, j].property == search_value)
          {
              O = ObjArray[i, j]; // you found it, now remember it
              break;
          }
      }
  }
  </code></pre>
  
  <p>No more need for that pesky extra boolean!</p>
  
  <p>Nevertheless, the search for alternative or better solutions goes on. Over the years I've tried many other ways, but find them not that great for one reason or another:</p>
  
  <ol>
  <li>Set <code>j</code> (the outer-loop iterator) to a value above <code>height</code>, which will trigger it to break automatically. Not ideal because <strong>sometimes you want to remember</strong> the <code>i</code> and <code>j</code> values where you found it.</li>
  <li>Use <code>foreach</code> on the 2D array. Not ideal since <code>foreach</code> <strong>won't let you manipulate the collection</strong> (delete or add to it, which very typically is the reason I search for the object anyway).</li>
  <li>Just put the 2 loops in a function that does nothing but find and return <code>O</code>. <code>return</code> effectively breaks both loops. Many times this is okay, but not always. I do this for very generic searches, but <strong>there are also quite a lot of "group searches"</strong> where I want to collectivize the traversing. In those cases, I find 2 or more objects (sometimes within the same 2D array), remember them, and only then break out of both loops.</li>
  <li>Use a <code>goto</code>? (Whoa, could this be the only legit use of a goto? It's surprisingly more readable than the <code>KeepLooping</code> flag, especially if we have 3 or more loops.) <strong>Not ideal because coworkers will scream bloody murder. And in C#, will there be correct garbage cleanup after the <code>goto</code>?</strong></li>
  <li>Throw a custom exception? idk, i've never tried it, but it looks less readable than my currently preferred way.</li>
  <li>Once you've found the right object, do all your object manipulation inside the inner loop, and then <code>return;</code> <strong>This could get messy fast</strong>. Sometimes the object manipulation involves its own loops.</li>
  </ol>
  
  <p>There is also a <strong>very clever 7th way</strong>, thanks to User_PWY:</p>
  
  <pre><code>int size = width*height; // save this so you dont have to keep remultiplying it every iteration
  for (int i = 0; i &lt; size; i++)
  {
     int x = i % width; // ingenious method here
     int y = i / width; // ingenious method here
  
     O = ObjArray[x, y];
  
     if (O != null)
         break; // woohoo!
  }
  </code></pre>
  
  <p>This effectively compacts the 2D array into one <code>for</code> loop for iteration. However, some criticism has pointed out that the mod and division operators are pretty slow in comparison to just i++ or j++, so it could be slower (remember we are dealing with 2D arrays of who-knows-what size). Like i commented, there should be a way to get the division and remainder in one operation because I'm pretty sure the x86 assembly code has DIV opcodes that store the quotient and remainder in separate registers, all in one DIV instruction. But how to do/use that in C#, idk.</p>
  
  <p>It would be nice if C# allowed you to name loops, such as <code>L1</code> and <code>L2</code>, and then do something like <code>L1.break()</code>; regardless of which loop you're inside. Alas...it cannot be done in this language. (<strong>Could there be some secret way of doing this using macros?</strong>) Is there ever gonna be a C# 6.0 with this feature implemented?</p>
  
  <p>Edit: in my opinion, I judge solutions on their elegance and speed. Remember we are dealing with nested loops, which can get exponentially huge. An extra operation or comparison could make a difference.</p>
  
  <p>Okay, well, tell me your preferred way, especially if it's something not listed here.</p>