C# equivalent to Java’s continue ?

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.

Leave a Comment