tags:

views:

1623

answers:

5

Should be simple and quick: I want a C# equivalent to the following Java code:

orig: for(String a : foo) {
  for (String b : bar) {
    if (b.equals("buzz")) {
      continue orig;
    }
  }
  // other code comes here...
}


Edit: OK it seems there is no such equivalent (hey - Jon Skeet himself said there isn't, that settles it ;)). So the "solution" for me (in its Java equivalent) is:

for(String a : foo) {
  bool foundBuzz = false;
  for (String b : bar) {
    if (b.equals("buzz")) {
      foundBuzz = true;
      break;
    }
  }
  if (foundBuzz) {
    continue;
  }
  // other code comes here...
}
+5  A: 

I don't believe there's an equivalent, I'm afraid. You'll have to either use a boolean, or just "goto" the end of the inside of the outer loop. It's even messier than it sounds, as a label has to be applied to a statement - but we don't want to do anything here. However, I think this does what you want it to:

using System;

public class Test
{
    static void Main()
    {
        for (int i=0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
               Console.WriteLine("i={0} j={1}", i, j);
               if (j == i + 2)
               {
                   goto end_of_loop;   
               }
            }
            Console.WriteLine("After inner loop");
            end_of_loop: {}
        }
    }
}

I would strongly recommend a different way of expressing this, however. I can't think that there are many times where there isn't a more readable way of coding it.

Jon Skeet
+2  A: 

In VB.Net, you could just have one while loop and one for loop and then exit the desired scope level.

In C#, maybe break; ?

That might break out of the inner loop and allow the outer loop to keep going.

Joel Coehoorn
A: 

I know this doesn't really answer your question, but in your example, wouldn't "break" have the desired effect?

BFree
In that exact example given it would, but when there's code after the inner loop which shouldn't be executed, break would execute it whereas "continue orig" wouldn't. I'm assuming Epaga actually wants the "continue orig" effect.
Jon Skeet
Good point, I knew I was missing something...
BFree
A: 

I think you're looking for the simple "continue" keyword... However, not being a Java guy I don't really get what the code snippet is trying to achieve.

Consider the following, though.

foreach(int i in new int[] {1,2,3,5,6,7})
{
    if(i % 2 == 0)
        continue;
    else
        Console.WriteLine(i.ToString());    
}

The continue statement on line 4 is an instruction to continue with the loop at the next value. The output here would be 1,3,5 and 7.

Replacing "continue" with "break", as follows,

foreach(int i in new int[] {1,2,3,5,6,7})
{
    if(i % 2 == 0)
       break;
    else
        Console.WriteLine(i.ToString());    
}

will give the output 1. Break instructs the loop to terminate, most commonly used when you want to stop processing when a condition is met.

I hope this gives you some of what you were looking for, but if not, feel free to ask again.

ZombieSheep
thanks, but i do know the difference between continue and break. ;-) the thing is: i have two nested loops. and if a certain condition in the inner loop occurs, i want to "continue" the outer loop.
Epaga
Sorry - did't mean to teach you to suck eggs - The fault is mine for not reading the question adequately...
ZombieSheep
A: 

You could do something like:

for(int i=0; i< foo.Length -1 ; i++) {
  for (int j=0; j< bar.Length -1; j++) {
    if (condition) {
      break;
    }
    if(j != bar.Length -1)
        continue;
    /*The rest of the code that will not run if the previous loop doesn't go all the way*/
  }
}
Sergio
the problem here is the rest of the code is AFTER the nested loop, not in it.
Epaga
whoops i just noticed in my first example i had the line wrong for the "//code here" comment. sorry.
Epaga
You could just set a flag before breaking, and check the flag after the loop. And if you really want your code to be formally 'structured' you set j = bar.Length rather than using 'break'.
Joel Coehoorn
j = bar.Length would run the current loop to the end, this is not what is intended ;)
Sergio