Error inserting code after a break [closed]

It’s gives me error after break in the second if sequence.

int value;
value=someMethod();
foreach(List<GameObject>lista in tilesOrganizadosPorColuna)
{
    bool found=false; 
      //DoSomething(); 
    if(value==1)
    {  
    //if value=1 do just first iteration.
    break; 
   }
}

if(!found)
//DoOtherThhink();
}

What’s the correct way to do it? (I want to stop the foreach loop not the if)Thanks. I tryed google, but that was the example that I found. (That a break inside if would stop foreach not just the if).

Answer

The lack of indentation makes this hard to read, but I figure you meant to do this:

bool found=false; 
int value;
value=...;

foreach(List<GameObject>lista in tilesOrganizadosPorColuna)
{
    if(value==1)
    {   
        found=true;
        break; 
    }
}

if(!found)
{
    //DoSomething()
}

You were missing a couple of brackets. I figure your IDE should have warned you of this.

I’m afraid your code doesn’t make a whole lot of sense though, since you’re not doing anything with what you’re iterating, so found will only be dependant on what value is initialized to.

Leave a Reply

Your email address will not be published. Required fields are marked *