1. 程式人生 > >【C#基礎】while迴圈和do-while迴圈

【C#基礎】while迴圈和do-while迴圈

1.While迴圈(先判斷迴圈條件,在執行迴圈體)

        static void Main(string[] args)
        {
            int index = 1;
            int sum = 0;
            while (index <= 10)
            {
                sum += index;
                index++;
            }
        }

2.do-while迴圈(不管迴圈條件是否滿足,都會執行一次迴圈體)

        static void Main(string[] args)
        {
            int index = 1;
            int sum = 0;
            do
            {
                sum += index;
                if (index == 10)
                {
                    Console.WriteLine(sum);
                }
                index++;
            } while (index <= 10);
        }