Using break with nested loops

Hey, trying to write a pretty simple piece of code, seeing if there's a bit more elegant way to do this in C#. I'm trying to write a nested for loop, and have a single break statement to break out of the highest for loop.

For instance, in Java this would be done like this:
outerloop: for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
if (condition_met)
break outerloop;

Does C# have a similar method for defining a different loop to break out of than then innermost one

TIA.

-Stephen


Answer this question

Using break with nested loops

  • Abongs

    There's nothing specific with C# to do that. You'd have to use a flag variable and use that in the for statement's condition statement if you wanted to use for loops.

    Maybe if you explain in detail what you're trying to do, rather than looking for a language semantic, someone might be able to offer more details guidance.

  • enric vives

    Assuming the expression computing condition_met isn't changing anything and doesn't rely on variable j, then the condition should be the same after you break out of the inner loop. Therefore you can get rid of that done variable and write it like this:

    for( int i = 0; i < 10; i++)
    {
      for( int j = 0; j < 10; j++)     if( condition_met )   break;
      if( condition_met )   break;
    }

    Of course this will only work if the condition_met expression doesn't alter anything and also doesn't rely on the value of j.

    Otherwise, if it was me, I'd write it like this:

    for( int i = 0; i < 10; i++)
    {
      boolean broke = false;
      for( int j = 0; j < 10; j++)
        if( condition_met )
        {
          broke = true;
          break;
        }
      if( broke )   break;
    }

    rather than making the boolean global to the outer loop, or just use goto as suggested earlier.

     

    Another way to go is to wrap it in a function

    private boolean InnerLoopBroke( int i)
    {
      for( int j = 0; j < 10; j++)    if( condition_met )  return true;
      return false;
    }

     . . .

    for( int i = 0; i < 10; i++)
    {
      if( InnerLoopBroke( i ) )    break;
    }

     or even like this:

    private boolean FindFirstOpenSlot( out int i, out int j )
    {
      for( int i = 0; i < 10; i++)
      {
        for( int j = 0; j < 10; j++)      if( slot_at_ij_is_open )  return true;
      }
      return false;
    }

     . . .

    int i = 0;
    int j = 0;
    if( FindFirstOpenSlot( i, j ) )    // do stuff, i & j point to the open slot


  • SPA

    As far as I know, you'd have to use a goto:


    for (int i=0; i<10; i++)
    {
    for (int j=0; j<10; j++)
    {
    if (condition_met)
    goto outsideloop;
    }
    }
    outsideloop:


    This is of course, frowned on, because the keyword is called "goto", but the code is effectively identical the the Java version.

    If you are opposed to using a goto, alternately you can force the outer loop to exit:


    for (int i=0; i<10; i++)
    {
    for (int j=0; j<10; j++)
    {
    if (condition_met)
    {
    i = 11;
    break;
    }
    }
    }




  • Redmanmc

    Well you could keep track of open spots and everytime you fill in a spot or an items is removed from a spot update the tracking list but this is arguably overdesigning.

  • frac

    James, I thought about doing the second method there, but I need to retain the values that break the loop, so that one is out of the question. I think I'll just use the goto. Generally I never use them, but it's a much more elegant way of doing the break than having loop condition variables, which I was using before.

    Peter, what I'm trying to do is pretty simple. I have a 3x3 grid, with spots being either open or closed. The grid is held in a two-dimensional matrix. The point of the loops is to go through and find the first open spot, then just break out and do some work with those values. If there's a cleaner way to do it than using the goto statement, let me know. I just don't like code looking like this:

    bool done = false;
    for (int i=0; i<10; i++)
    {
    for (int j=0; j<10; j++)
    if (condition_met)
    {
    done = true;
    break;
    }
    if (done)
    break;
    }

  • Chabs

    I think I like the idea of wrapping it in a function, as this leads to the most graceful code solution yet. I already have a tiny little class called point which just holds an x & y coordinate for any location on the grid. I use it merely because I absolutely hate having to have parallel data arrays or other structures. Here's what my final function looks like:

    public point nextOpen()
    {
    for (int i=0; i<10; i++)
    for (int j=0; j<10; j++)
    if (condition_met)
    return (new point(i,j));
    return null;
    }

  • Robert Palmer

    Wrapping it in a function would be my preferred solution too.
  • waheyluggage

    The equivalent of this in C# would be to use a goto but personally I don't like these COBOL situations to control the execution path:



  • Using break with nested loops