工控隨筆_C#連接PLC_之_C#入門_06_流程控制


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

namespace Flow
{
    class Program
    {
        static void Main(string[] args)
        {
            #region if語句
            //if語句
            if(true)
            {
                Console.WriteLine("00 因為if語句的條件為真,所以執行這個代碼塊的程序");
            }

            if(false)
            { 
                Console.WriteLine("01 因為if語句條件為真假,所以這個塊里面代碼不會執行");
            }
            

            if (true)
            {
                Console.WriteLine("02 因為if...else...語句的條件為真,所以執行if分支");
            }
            else
            {
                Console.WriteLine("03 因為if...else...語句的條件為真,所以不執行else分支");
            }

            if (false)
            {
                Console.WriteLine("04 因為if...else...語句的條件為假,所以不執行if分支");
            }
            else
            {
                Console.WriteLine("05 因為if...else...語句的條件為假,所以執行else分支");
            }
            #endregion

            Console.WriteLine();

            for (int i = 0; i < 5;i++ )
            {
                Console.WriteLine("for循環第 {0} 次",i+1);
            }
            Console.WriteLine();
            int j=0;
            while (j < 5)
            {
               Console.WriteLine("while循環第 {0} 次", ++j);
            }
            Console.WriteLine();
            int k=0;
            do
            {
                Console.WriteLine("do...while循環第 {0} 次", ++k);
            }
            while (k<5);

            Console.WriteLine();
            //利用continue關鍵字輸出30以內的奇數
            for(int odd=0; odd<30; odd++)
            {
                // % 用來求兩個整數的余數,即求模
                if(odd % 2 == 0)
                {
                    continue ;
                }
                else
                {
                    Console.WriteLine("這是0到30之內的第 {0} 個奇數: {1}", odd/2+1, odd);
                }
            }

             Console.WriteLine();
            //利用break語句跳出循環
            int iBreak = 0;
            while (true)
            {
                Console.WriteLine("這是死循環,如果不用特殊的手段則不能跳出來");

                if (iBreak >= 5)
                {
                    Console.WriteLine("在循環內部,循環執行完畢的條件已經滿足,即將跳出循環");
                    break;
                }
                iBreak++;
            }

            Console.WriteLine();
            //利用goto語句實現循環
            int igoto = 0;
            label1:
                Console.WriteLine("在goto語句循環內部");
                igoto++;
                if (igoto < 3)
                {
                    Console.WriteLine("goto循環條件還滿足,繼續循環");
                    goto label1;
                }

            Console.WriteLine();
            //利用return 語句跳出循環
            int iReturn = 0;
            for ( ; ; )
            {
                iReturn++;
                if (iReturn >= 5)
                {
                    Console.WriteLine("利用return語句結束程序的執行");
                    Console.WriteLine("最后的請輸入字符的語句不會執行了");
                    Console.ReadKey();
                    return;
                }
            }

            Console.WriteLine("請輸入一個字符:");
            Console.ReadKey();
        }
    }
}

 

 

--------------------------------------------------------------分割線---------------------------------------------------------------

1、文章均為個人原創,歡迎轉載,轉載請保留出處:https://www.cnblogs.com/volcanol/

2、獲取工控PLC、變頻器、HMI、計算機、Windows、Linux、嵌入式資料點擊:獲取資料

3、如果您覺得文章對您有幫助可轉至頁面上半部分打賞,或移步:打賞 

4、或者在頁面右下角點推薦喲!!!

--------------------------------------------------------------分割線---------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM