3

Possible Duplicate:
continue and break with an extended scope

i have one problem. I don't know how to call continue for "for" inside foreach.

Code:

for(int i = 0; i < ...; i++)
{
  foreach(´´ .. in ´´)
  {
    if(,,.Value == null)
      // A!
  }
}

And i need replace "A!" for code to continue "for"! Not foreach. I used basic continue but it work just only for foreach.

SOLVED with:

for(int i = 0; i < ...; i++)
{
  bool Ab = false;

  foreach(´´ .. in ´´)
  {
    if(,,.Value == null)
      Ab = true;
  }

  if(Ab)
    continue;
}
Community
  • 1
  • 1
user1085907
  • 1,009
  • 2
  • 16
  • 40
  • 3
    What sort of object are you looping through? There might be a far better way than using the foreach... if... anti-pattern. – Stefan H Jul 25 '12 at 16:55
  • You can't, you need to use a variable to pass that information out to the for loop. As an aside, you'll get better responses if you accept some answers. – Jodrell Jul 25 '12 at 16:56
  • Try `break` instead of continue in your foreach loop, this will cause the execution to fall out of the `foreach` and continue the `for` loop – Nathan Jul 25 '12 at 16:56
  • I think [this](http://stackoverflow.com/questions/982595/how-to-break-out-of-2-loops-without-a-flag-variable-in-c) could be the (probably unsatisfying) answer. – Johannes Egger Jul 25 '12 at 16:56

4 Answers4

3

if you need to stop the inner loop, and continue the outer loop, you just need a break statement in the inner loop

for(int i = 0; i < ...; i++)
{   
    foreach(var thing in something)   
    {     
        if(thing.Value == null)       
        {
            break; 
        } 
    }

}

John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • but how i can call continue on break? Is any "if" on this? – user1085907 Jul 25 '12 at 17:46
  • the `break' statement stops the inner loop (what you want) and then continues the outer loop (what you want), so i'm not sure what more you need. If you have some specific need that this doesn't work, you need to post more code in your question so we know exactly what you're trying to do. – John Gardner Jul 25 '12 at 18:57
1

If that is your actual code structure, you can break from the foreach and the for will immediately continue to the next iteration because the current one is over.

If, however, you have more code in the for iteration after the completion of the foreach loop, you may be in one of those rare cases where (mothers, shield your child's ears) you might want to use a goto to skip to the end of the current for iteration.

The cleaner way, though, would be to fashion your internal loop so that it occurs at the end of your outer loop, such that a break in the internal loop is a continue of the outer loop.

Omaha
  • 2,262
  • 15
  • 18
  • *Pouring water on my burning ears*. Yeah, they give you the `goto` command because they know if they didn't, then someone, somewhere would come up with a situation that simply could not be solved any other way. But, the last paragraph is the "teaching answer"; the one that should be given to someone trying to learn how to code right. Use of `goto` is a very stinky code smell, anytime, anywhere. – KeithS Jul 25 '12 at 17:19
0

Can't you just break the inner foreach loop?

for (int i = 0; i < blah; i++) {
  foreach (var item in collection) {
    if (ShouldContinue(item)) {
      break;
    }
    // other stuff
  }
  // don't put things here
}

If you need code where I put don't put things here it's probably far clearer if you wrap your inner loop in a function:

bool ProcessOrContinue(IEnumerable collection) {
  foreach (var item in collection) {
    if (ShouldContinue(item)) {
      return true;
    }
    // other stuff
  }
  return false;
}

for (int i = 0; i < blah; i++) {
  if (ProcessOrContinue(collection)) continue;
  // more things
}
Ron Warholic
  • 9,994
  • 31
  • 47
0

Continue is a fancy goto. You could make use of recursion here instead.

danish
  • 5,550
  • 2
  • 25
  • 28