十一、流程控制之goto語句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _11.流程控制之goto語句
{
    class Program
    {
        static void Main(string[] args)
        {
            // goto語句用於控制代碼何時執行。
            
            /** 
             * goto語句使用語法:
             *  goto <labelname>;
             * goto語句標籤名定義語法:
             *  <labelname>:
             */
             
            // 使用goto語句會導致程序流程混亂,代碼不容易閱讀。
            
            // 使用goto語句可能導致有些代碼塊無法執行。
            
            // 使用goto語句實現1+...+10累加功能。
            
            {
                int i = 1, sum = 0;
                
            Loop:
                sum += i;
                i++;
                if (i > 10)
                    goto End;
                goto Loop;
            End:
                Console.WriteLine("sum = {0}", sum);
            }
            
            {
                int myInteger = 5;
                
                goto myLabel;
                myInteger += 10; // 此句代碼無法執行
            myLabel:
                Console.WriteLine("myInteger = {0}", myInteger);
            }
            
            Console.ReadKey();
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章