Use goto statement to jump out of a for loop in CSharp

Description

The following code shows how to use goto statement to jump out of a for loop.

Example


using System;/*from   w  w  w. j a v a2 s . co  m*/

public class MainClass {     
    static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            if (i == 1)
            {
                goto end;
            }
        }
    end: Console.WriteLine("The end");

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception