Goto Statement in C#

Goto Statement in C# with Examples

In this article, I am going to discuss Goto Statement in C# with Examples. Please read our previous articles, where we discussed Continue Statement in C# with Examples. At the end of this article, you will understand the goto Statement in C# and when and how to use the goto statement in the C# language with examples.

Goto Statement in C#

The Goto Statement in C# is used to transfer the control to the labeled statement in the program. The label is a valid identifier and placed just before the statement from where the control is transferred. That means the goto Statement provides an unconditional jump from the goto to a labeled statement in the same function.

Goto is a keyword and by using this goto keyword we can pass the control anywhere in the program in the local scope. When we are working with the goto statement it required an identifier called a label. Any valid identifier followed by a colon is called a label. Whenever we are working with a goto statement it is called an unstructured control flow statement because it breaks the rule of structure programming language.

The goto statement is rarely used because it makes the program confusing, less readable, and complex. Also, when it is used, the control of the program won’t be easy to trace, hence it makes testing and debugging difficult.

Goto Statement Flowchart:

Goto Statement Flowchart

Syntax to use goto statement in C# Language:

Syntax to use goto statement in C# Language

Example to Understand the goto statement in C# Language:
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("1st Statement: ");

            goto skip;   //goto label skip:

            Console.WriteLine("2nd Statement: ");

             skip:    //label:
            Console.WriteLine("3rd Statement: ");

            Console.ReadKey();
        }
    }
}
Output:

Example to Understand the goto statement in C# Language

The goto statement transfers program control to a labeled statement. The label statement must exist in the scope of the goto statement. More than one goto statement can transfer control to the same label. This statement can be used to get out from a loop or an inner nested loop to an outer loop. For a better understanding, please have a look at the below example.

using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i == 5)
                {
                    goto Exitlabel;
                }
                Console.WriteLine(i);
            }
            Console.WriteLine("Next statement placed after loop");

            Exitlabel: //goto label
                Console.WriteLine("Labeled statement");

            Console.ReadKey();
        }
    }
}
Output:

Goto Statement in C# with Examples

Unlike the break statement, it does not transfer the program control to the next statement which is placed immediately after the loop or switch. You can also use the goto statement to transfer control to a specific switch-case label or the default label in a switch statement. For a better understanding, please have a look at the below example.

using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 20;
            switch (number)
            {

                case 5:
                    Console.WriteLine("case 5");
                    break;
                case 10:
                    Console.WriteLine("case 10");
                    break;
                case 20:
                    Console.WriteLine("case 20");
                    // goto statement transfer
                    // the control to case 5
                    goto case 5;

                default:
                    Console.WriteLine("No match found");
                    break;
            }
            Console.ReadKey();
        }
    }
}
Output:

Goto Statement in C# with Examples

Note: Like break and continue statement we should avoid using “goto Statement” where it’s possible. it is very difficult to trace out the way of execution and figure out what the program is doing. and debugging and modifying such a program is very difficult. And all of our Programs Should consist of sequences, decisions, and loops.

Some tricky questions related to the C# goto statement

Question1: What will be the output of the below program?
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("C#Tutorials ");
            Console.Write("Welcome ");
            XYZ:
                Console.Write("X ");
                Console.Write("Y ");
                goto ABC;
                Console.Write("Programming ");
            ABC:
                Console.Write("Hello1 ");
                Console.Write("Hello2");

            Console.ReadKey();
        }
    }
}

Output: C#Tutorials Welcome X Y Hello1 Hello2

Note: In order to execute the program if the label has occurred it will be executed automatically without calling also. The creation of labels is always optional, after creating the label calling the label is also optional.

So, whenever we need to repeat the statement “n” number of times without using loops then we can use the goto statement but in the goto statement, we cannot place the break and continue statement.

Question2: What will be the output of the below program?
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 2;
            EVEN:
                Console.Write($"{i} ");
                i += 2;
                if (i <= 20)
                    goto EVEN;

            Console.ReadKey();
        }
    }
}

Output: 2 4 6 8 10 12 14 16 18 20

Question3: What will be the output of the below program?
using System;
namespace JumpStatementDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("A");
            Console.Write("CTutorials");
            goto ABC;
            Console.Write("WELCOME");
            Console.Write("HELLO");
            abc:
                Console.Write("B");
                Console.Write("C");

            Console.ReadKey();
        }
    }
}

Output: Error CS0159 No such label ‘ABC’ within the scope of the goto statement

In the goto statement, labels work with the help of case sensitivity i.e. upper case label and lower case label both are different.

In the next article, I am going to discuss Functions in C# with Examples. Here, in this article, I try to explain Goto Statement in C# Language with Examples. I hope you enjoy this Goto Statement in C# with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.