C# Goto Statement
To transfer the control to a specific part of the code from a deeply nested loop or a switch case label, the C# goto statement is used. It is also known as a jump statement because it jumps to a particular label unconditionally. The Goto statement, however, makes a program complex and is thus not recommended to use frequently in C#.
Example:
using System; public class Example { public static void Main(string[] args) { ineligible: Console.WriteLine("Not eligible for the exam!"); Console.WriteLine("Enter your marks:\n"); int marks = Convert.ToInt32(Console.ReadLine()); if (marks < 80){ goto ineligible; } else { Console.WriteLine("Eligible for the exam!"); } } } |
Output:
Explanation:
In the above example, we are displaying the behavior and working of the Goto statement in C#.