The goto Statement


A goto statement may be used to unconditionally transfer to a labelled statement. For example, consider the following.

// GoTo - A demonstration of the goto statement.

using System;

    class Program
    {
        static void Main()
        {
            int i=0, j = 0;
            for (; i < 100; i++)
            {
                j += i;
                if (i == 50) goto label;
            }

        label:
            Console.WriteLine("The sum of 0 to {0} is {1}", i, j);
        }
    }

The output of the program is:

The sum of 0 to 50 is 1275

The goto within the for loop unconditionally transfers control of execution to the labelled statement. A labelled statement is designated by valid C# identifier followed by a colon (as above).