CS Goto Statement

The goto statement is a jump statement, which is also called an unconditional jump statement sometimes. The Goto declaration can be used to jump from anywhere within a function.

The C# goto statement is also renowned for jumping. It is used for controlling the other portion of the program. It jumps to the specified label unconditionally. It can be used for transferring control from a deep loop or switching case label.

Using goto declaration in C# is currently prevented because it makes the program complicated.

Example:


using System;
public class GotoExample
{
    public static void Main(string[] args)
    {
    ineligible:
        Console.WriteLine("opps! 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("Hi! Now you are eligible to vote!");
        }
    }
}

 

 

Output

opps! You are not eligible to vote!
Enter your age:

17
opps! You are not eligible to vote!
Enter your age:

When Enter age graterthan 17

opps! You are not eligible to vote!
Enter your age:

18
Hi! Now you are eligible to vote!