C# Goto Statement - Goto Statement in C#



C# Goto Statement

 cshrap-goto-statement
  • It is used to transfer control to the other part of the program and unconditionally jumps to the specified label. So, it is also known as jump statement.
  • It can be used to transfer control from deeply switch case label or nested loop.
  • In C# it is avoided to use Goto statement because it makes the program complex.

Sample Code

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

namespace Goto_Statement
{
    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!");
            }
        }
    }
}

Output

You are not eligible to vote!Enter your age: 12
You are not eligible to vote!
Enter your age: 18
You are eligible to vote

Related Searches to C# Goto Statement - Goto Statement in C#