By 22/07/2015 9 Comments

C# Goto Statement

  • C# goto statement is used to jump directly from one program’s part to another unconditionally .
  • goto statement uses a label for jumping process. A label is a valid or meaningful string name followed by a colon and it specifies or point out that location where goto statement will jump.
  • goto statement cannot jump outside of a class and into code blocks such as loops etc.

Example – 1:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // for loop declaration.
            for (int countNumber = 1; countNumber <= 10; countNumber++)
            {
                // if statement which checks a condition.
                if (countNumber == 6)
                    goto stop; // goto statement, execution is redirected to stop label name.

                // Print countNumber variable.
                Console.WriteLine("Number: {0}", countNumber);
            }

        // A user defined label name.
        stop:

            // When above loop is terminated the following statement will be printed.
            Console.WriteLine("Loop has terminated at number 5 by goto statement.");

            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
    The Output will be:
    Number: 1
    Number: 2
    Number: 3
    Number: 4
    Number: 5
    Loop has terminated at number 5 by goto statement.
  */
}


Example – 2:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An int type variable is initialized.
            int count = 1;
            
        // A label declaration.
        Repeat:

            // if statement.
            if (count <= 10)
            {
                // Print count variable.
                Console.WriteLine("Line: {0}", count);

                // count variable is incremented each time.
                count++;

                // Execution go back to Repeat label.
                goto Repeat;
            }
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
    The Output will be:
    Line: 1
    Line: 2
    Line: 3
    Line: 4
    Line: 5
    Line: 6
    Line: 7
    Line: 8
    Line: 9
    Line: 10
  */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .