C# Goto Statement with Examples

  Keywords : C# goto statement or jump statement to transfer control from one point to another point with examples, C# goto statement to go out of the loop or inner nested loop to outer loop with example

Here we will learn goto statement in c# with examples, use c# goto statement in for loop with examples, use c# goto statement in switch case statement with examples and how to use goto statement in c# to transfer a program control to the specified labeled statement with examples.

C# Goto Statement

In c#, Goto statement is used to transfer a program control to the defined labeled statement and it is useful to get out of the loop or exit from a deeply nested loops based on our requirements.

 

Generally, in c# the defined labeled statement must always exists in the scope of goto statement and we can define multiple goto statements in our application to transfer the program control to specified labeled statement.

 

For example, we can use goto statement in switch statement to transfer a control from one switch-case label to another or to a default label based on our requirements.

Syntax of C# Goto Statement

Following is the syntax of defining a goto statement in c# programming language.

 

goto labeled_statement; 

If you observe above syntax we defined a goto statement by using goto keyword and with labeled_statement. Here the labeled_statement is used to transfer the program control to specified labeled_statement position.

 

Now we will see how to use goto statement in c# for loop to get out of the loop at particular condition with examples

C# Goto Statement with For Loop Example

Following is the example of using goto statement in for loop to exit the loop based on our requirements.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i < 10; i++)

            {

                if (i == 5)

                {

                    goto endloop;

                }

                Console.WriteLine("i value: {0}", i);

            }

            endloop: Console.WriteLine("The end");

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a goto in for loop with labeled statement “endloop” to exit for loop whenever the variable (i) value equals to 5.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Goto Statement with For Loop Example Result

 

If you observe above result, whenever the variable (i) value equals to 5, the goto statement transferred the program control from for loop to specified label statement (endloop) position.

C# Goto Statement with Switch Statement

In c#, we can use goto statement exit from defined loops or transfer a control to specific switch-case label or the default label in switch statement based on our requirements.

 

Now we will see how to use goto statement in switch-case statement with example. Following is the example of using goto with switch-case statement to transfer a control from one switch-case label to another based on our requirements.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 3, j = 0;

            switch (i)

            {

                case 1:

                    j += 20;

                    Console.WriteLine("j value is {0}",j);

                    break;

                case 2:

                    j += 5;

                    goto case 1;

                case 3:

                    j += 30;

                    goto case 1;

                default:

                    Console.WriteLine("Not Known");

                    break;

            }

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a goto statement in multiple switch cases and trying to transfer program control from case 2 / case 3 to case 1.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Goto Statement with Switch Case Statement Example Result

 

 This is how we can use goto statement with switch-case statements to transfer the program control from one case to another in c# programming language based on our requirements.

 

It’s better to avoid using goto statement in our c# applications because it will make the program logic complex and it’s a difficult to understand the process flow of program execution.