C#循環及運算符 for循環

多餘不說,直接上代碼,其實就是突然想起來,然後對某些東西的一個整理,

其中Console.WriteLine顯示信息,Console.ReadLine 就類似個暫停鍵,按Enter繼續

後面有此部分代碼運行出的結果

 

            #region 循環及運算符(部分)
        
            #region 三目運算符
            int ten = 101;
            int ifel = ten < 100 ? ten : 100;
            Console.WriteLine("三目運算符:" + ifel);
            #endregion

            #region Switch 運算
            Console.WriteLine("Switch 運算");
            int SwitchKey = 100;
            switch (SwitchKey)
            {
                case 10:
                    Console.WriteLine("It is 10");
                    break;
                case 12:
                    Console.WriteLine("It is 12");
                    break;
                default:
                    Console.WriteLine("It is default");
                    break;
            }
            #endregion

            #region for 運算
            for (int ia = 0; ia < 3; ia++)
            {
                Console.WriteLine("通常的for循環寫法,ia={0}", ia);
            }
            Console.WriteLine("----------------------");
            Console.WriteLine("稍微複雜些的for循環寫法");
            int i;
            int j = 10;
            for (i = 0, Console.WriteLine("sfs"); i < j; i++, j--, Console.WriteLine("i={0},j={1}", i, j))
            {
                Console.WriteLine("for循環中,i={0}", i);
            }
            Console.WriteLine("----------------------");
            Console.WriteLine("奇葩的for循環寫法");
            bool stop = false;
            for (; !stop; )
            {
                stop = true;
                Console.WriteLine("can u stop"); //只運行一次
            }
            Console.WriteLine("----------------------");
            //for (; ; ) //可編譯成功
            //{
            //    Console.WriteLine("can u stop"); //運行時此處死循環
            //}
            Console.ReadLine();
            #endregion

            #region foreach 運算
            Console.WriteLine("----------------------");
            //IEnumerable IEnumerable
            List<int> listInt = new List<int> { 1, 2, 3 };
            foreach (var intInList in listInt)
            {
                Console.WriteLine(intInList);
            }
            #endregion

            #region where 運算
            Console.WriteLine("----------------------");
            int n = 1;
            n = 1;
            Console.WriteLine("n is 1");
            while (++n < 6)
            {
                Console.WriteLine("n  n is {0}", n);
            }
            Console.WriteLine();
            n = 1;
           //n++ n=n+1
            Console.WriteLine("n is 1");
            while (n++ < 6)
            {
                Console.WriteLine("n++ n is {0}", n);
            }
            Console.WriteLine();
            n = 1;
            while (++n < 6)
            {
                Console.WriteLine("++n n is {0}", n);
            }
            #endregion

            #region   do where 運算
            int DoWhile=9;
            do
            {
                Console.WriteLine("do while");
            }
            while (false);

            #endregion

            #region Condition
            object nullObject = false;
            //object nullObject = null;
            if (!false || (bool)nullObject)  //備註: "||"的前面如成立則不會運行到 "||"後面
            {
                Console.WriteLine("True Condition");
            }
            else
                Console.WriteLine("False Condition");

            if (!false | (bool)nullObject)
            {
                Console.WriteLine("True Condition");
            }
            else
                Console.WriteLine("False Condition");
            Console.ReadLine();
            #endregion

            #endregion

以上代碼運行結果截圖如下:

 

 

 

 

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