C# Goto Statement

C# Goto Statement

The C# jump statements are break, continue, goto, return, and throw.

1 - C# Goto Statement

The goto statement transfer execution to another label within a statement block.

The C# goto statement is also known jump statement. It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label.

It can be used to transfer control from deeply nested loop or switch case label.

Currently, it is avoided to use goto statement in C# because it makes the program complex.

Syntax of C# Goto Statement

The goto statement has the following syntax:

goto statement-label;

Or, when used within a switch statement:

goto case case-constant;

A label is a placeholder that precedes a statement, denoted with a colon suffix.

You can only goto to constants branch in switch statement, not patterns.

A label is a marker for a code block.

You can add a label as follows:

startLoop:

The following code iterates the numbers 1 through 5. It uses if and goto to implement a loop:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            startLoop:
            if (i <= 5)
            {
                Console.WriteLine("value oh i: "+i);
                i++;
                goto startLoop;
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Goto Statement - XDevSpace

 

2 - C# Goto Statement Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            ineligible:
            Console.WriteLine("You are not eligible to vote!");

            Console.WriteLine("Enter your age:\n");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age < 18)
            {
                goto ineligible;
            }
            else
            {
                Console.WriteLine("You are eligible to vote!");
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Goto Statement Example - XDevSpace

 

3 - C# Goto Statement with For Loop Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    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 Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Goto Statement with For Loop Example - XDevSpace

4 - C# Goto Statement with Switch Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    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 Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Goto Statement with Switch Statement - XDevSpace