C# equivalent to Java's continue <label>?

c#, java

Solution

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.

Problem

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... } ```

Original source