close
每個程式常常在連續的步驟後,會遇到要停下來,判斷各種情況後,在繼續往下執行。底下是一些常用到的判斷語法。


 
Control Statements – Seelction
The if Statement
取決於給予的條件,來執行不同的程序,當條件是 true 時,將執行那區塊的程式碼。
 
syntax:
if () {
    statements;
} else if () {
….
} else {
        statements;
}
 
if (money > 0) {
        Console.WriteLine(“時間就是金錢({0}),朋友!”, money);
} else {
        Console.WriteLine(“天啊!你真窮。”);
}
 
當有多種情況需要判別時,就會衍生成下面的樣子
 
if (xx > 100) {
        statements;
} else if (xx<100 && xx > 50) {
        statements;
} else if (xx<50 && xx>20) {
        statements;
} else {
        statements;
}
 
The switch Statement
取決於給予的一整組邏輯的值,當符合其特定的參數,其包含的區塊成勢將會被執行。
switch 運算式的值可以為 booleansenumsintegral typesStrings
 
syntax:
switch ()
{
case value1:
        statements;
        break;
    case value2:
        statements;
        break;
    …
    default:
        statements;
        break;
}
 
int myInt;
 
    myInt = Console.ReadLine();
    // switch with integer type
    switch (myInt)
    {
        case 1:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
        case 2:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
        case 3:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
        default:    // 當都不符合上面條件時,就會執行此區塊
            Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
            break;
    }
 
Control Statements – Loops
當要重複執行某些特定的動作,loops是最後達成這任務的。
 
The for Loop
for loop包含三個部分:起始值、條件判斷和條件增量。
syntax:
for (; ; ) {
;
}
 
ForLoop.cs
    using System;
 
    class ForLoop
    {
        public static void Main()
        {
            for (int i=0; i < 20; i++)
            {
                if (i == 10)
                    break;
 
                if (i % 2 == 0)
                    continue;
 
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
        }
    }
 
Output:
1 3 5 7 9
 
說明:
1.      i的起始值為0;條件式是小於20i的增量每次加1
2.      i等於10的時候就會跳出for loop
3.      i2整除時,i直接加1
 
The while Loop
while loop就包含一個條件式,當條件一直成立(true)時,就是一直重複執行那段程式。
 
syntax:
while () {
;
}
 
WhileLoop.cs
    using System;
 
    class WhileLoop
    {
        public static void Main()
        {
            int myInt = 0;
 
            while (myInt < 10)
            {
                Console.Write("{0} ", myInt);
                myInt++;
            }
            Console.WriteLine();
        }
    }
 
The do Loop
do loop動作和while loop差不多,最明顯的差異在於 do loop最少會執行一次。
 
syntax:
do
{
;
} while ();
 
DoWhileLoop.cs
    using System;
 
    class DoWhileLoop
    {
        public static void Main()
        {
            int myInt = 0;
 
            do
            {
                Console.Write("{0} ", myInt);
                myInt++;
            } while (myInt < 10);
            Console.WriteLine();
        }
    }
 
總結:
Selection Control Statements讓你可以用不同的判斷式來執行不同的邏輯分支的程式。
Loop Control Statements 讓你可以輕鬆重複執行同一區塊的程式。

不同的情況選擇適當的 Control Statement 將會使程式更易於閱讀和執行工作,達到事半功倍的效用。

arrow
arrow
    文章標籤
    C# Control Statements
    全站熱搜

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