close

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;
            }
        }
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 imagefish 的頭像
    imagefish

    幻想魚的幻想空間

    imagefish 發表在 痞客邦 留言(0) 人氣()