Using break with nested loops  
Author Message
Stephen Fulwider





PostPosted: Visual C# Language, Using break with nested loops Top

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


Visual C#1  
 
 
Gabriel Lozano-Moran





PostPosted: Visual C# Language, Using break with nested loops Top

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:



 
 
Peter Ritchie





PostPosted: Visual C# Language, Using break with nested loops Top

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.

 
 
James Curran





PostPosted: Visual C# Language, Using break with nested loops Top

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;
}
}
}




 
 
Stephen Fulwider





PostPosted: Visual C# Language, Using break with nested loops Top

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;
}

 
 
Gabriel Lozano-Moran





PostPosted: Visual C# Language, Using break with nested loops Top

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.

 
 
Axe22





PostPosted: Visual C# Language, Using break with nested loops Top

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


 
 
Stephen Fulwider





PostPosted: Visual C# Language, Using break with nested loops Top

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;
}

 
 
Paul Louth





PostPosted: Visual C# Language, Using break with nested loops Top

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