This is a simple sample for Exit statement.
static void Main(string[] args)
{
Program.correctGoto();
Program.GotoInfiniteLoop();
Program.falseGoto();
}
//This is a correct Goto statement method.
static public void correctGoto()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("{0}", i);
if (i == 5)
{
goto Exit;
}
}
Exit: Console.WriteLine("Exit");
}
//If you put Exit tag before goto statement, it will cause infinite loop.p>
static public void GotoInfiniteLoop()
{
Exit: Console.WriteLine("Exit");
for (int i = 0; i < 100; i++)
{
Console.WriteLine("{0}", i);
if (i == 7)
{
goto Exit;
}
}
}
//It's a wrong method to use exit statement.
static public void falseGoto()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("{0}", i);
if (i == 7)
{
Exit: Console.WriteLine("Exit");
}
// this method can't jump from external into internal.
goto Exit;
}
}
文章標籤
全站熱搜
