if-else (C# 參考)

執行的陳述式會根據運算式的 Boolean 值的 if 陳述式識別。 在下列範例中,Boolean 變數 result 設為 true,然後以 if 陳述式進行檢查。 輸出為 The condition is true。

bool condition = true;

if (condition)
{
    Console.WriteLine("The variable is set to true.");
}
else
{
    Console.WriteLine("The variable is set to false.");
}

您可以在本主題中的範例會將它們負責在主控台應用程式的 Main 方法。

如下列範例所示,是 if 陳述式在 C# 中可能有兩種形式。

// if-else statement
if (condition)
{
    then-statement;
}
else
{
    else-statement;
}
// Next statement in the program.


// if statement without an else
if (condition)
{
    then-statement;
}
// Next statement in the program.

在 if-else 陳述式,則為,否則為 true 的 condition 評估為 then-statement ,執行。 如果 condition 為 false, else-statement 執行。 由於 condition 無法同時為 true 和 false, then-statement 和 if-else 陳述式的 else-statement 絕不能兩個回合。 在 then-statement 或 else-statement 執行後,控制權會轉移到下一個陳述式在 if 陳述式之後。

在不包含 else 陳述式的 if 陳述式,則為,如果 condition 為 true 時, then-statement 執行。 如果 condition 為 false,控制權會轉移到下一個陳述式在 if 陳述式之後。

在括號內的 then-statement 和 else-statement 可包含單一陳述式或多個陳述式 ({}) 中。 對於單一陳述式,括號是選擇項,但建議使用。

陳述式在 then-statement 和 else-statement 可以是任何型別,包括其他 if 陳述式巢狀在原始的 if 陳述式內。 在巢狀 if 陳述式,沒有對應的 else的每個 else 子句隸屬於最後一個 if 。 在下列範例中, Result1 似乎,如果 m > 10 和 n > 20 都評估為 true。 如果 m > 10 為 true,但 n > 20 是 false,則 Result2 會出現。

// Try with m = 12 and then with m = 8. 
int m = 12;
int n = 18;

if (m > 10)
    if (n > 20)
    {
        Console.WriteLine("Result1");
    }
    else
    {
        Console.WriteLine("Result2");
    }

如果為,則相反地,您希望 Result2 發生在 (m > 10) 為 false,您可以指定使用括號的關聯建立巢狀 if 陳述式的開頭和結尾,如下列範例所示。

// Try with m = 12 and then with m = 8. 
if (m > 10)
{
    if (n > 20)
        Console.WriteLine("Result1");
}
else
{
    Console.WriteLine("Result2");
}

Result2 ,就會出現條件 (m > 10) 評估為 false。

範例

在下列範例中,將由鍵盤輸入的字元,因此,程式使用巢狀 if 陳述式判斷輸入字元是否為字母字元。 如果輸入字元是字母字元,程序會檢查輸入的字元是大寫或小寫。 訊息為每一種情況下發生。

Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
    if (Char.IsLower(c))
    {
        Console.WriteLine("The character is lowercase.");
    }
    else
    {
        Console.WriteLine("The character is uppercase.");
    }
}
else
{
    Console.WriteLine("The character isn't an alphabetic character.");
}

//Sample Output: 

//Enter a character: 2 
//The character isn't an alphabetic character. 

//Enter a character: A 
//The character is uppercase. 

//Enter a character: h 
//The character is lowercase.

因為下列部分程式碼所示,您也可以巢狀方式置於其他區塊內的 if 陳述式。 在兩個區塊和一個內部的範例的 if 陳述式會封鎖。 註解指定的條件為 true 或 false 的在每個區塊。

// Change the values of these variables to test the results. 
bool Condition1 = true;
bool Condition2 = true;
bool Condition3 = true;
bool Condition4 = true;

if (Condition1)
{
    // Condition1 is true.
}
else if (Condition2)
{
    // Condition1 is false and Condition2 is true.
}
else if (Condition3)
{
    if (Condition4)
    {
        // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
    }
    else
    {
        // Condition1, Condition2, and Condition4 are false. Condition3 is true.
    }
}
else
{
    // Condition1, Condition2, and Condition3 are false.
}

下列範例會判斷編碼字元是否為小寫字母、大寫字母或數字。 如果這三個條件為 false,字元不是英數字元。 這個範例會顯示每個案例的訊息。

Console.Write("Enter a character: ");
char ch = (char)Console.Read();

if (Char.IsUpper(ch))
{
    Console.WriteLine("The character is an uppercase letter.");
}
else if (Char.IsLower(ch))
{
    Console.WriteLine("The character is a lowercase letter.");
}
else if (Char.IsDigit(ch))
{
    Console.WriteLine("The character is a number.");
}
else
{
    Console.WriteLine("The character is not alphanumeric.");
}

//Sample Input and Output: 
//Enter a character: E 
//The character is an uppercase letter. 

//Enter a character: e 
//The character is a lowercase letter. 

//Enter a character: 4 
//The character is a number. 

//Enter a character: = 
//The character is not alphanumeric.

如同在為區塊中的陳述式或區塊可以是任何有效的陳述式,您可以為這個情況使用任何有效的布林運算式。 您可以使用邏輯運算子,例如 &&&|||並進行複雜條件的 ! 。 下列程式碼顯示範例。

// NOT
bool result = true;
if (!result)
{
    Console.WriteLine("The condition is true (result is false).");
}
else
{
    Console.WriteLine("The condition is false (result is true).");
}

// Short-circuit AND
int m = 9;
int n = 7;
int p = 5;
if (m >= n && m >= p)
{
    Console.WriteLine("Nothing is larger than m.");
}

// AND and NOT
if (m >= n && !(p > m))
{
    Console.WriteLine("Nothing is larger than m.");
}

// Short-circuit OR
if (m > n || m > p)
{
    Console.WriteLine("m isn't the smallest.");
}

// NOT and OR
m = 4;
if (!(m >= n || m >= p))
{
    Console.WriteLine("Now m is the smallest.");
}
// Output:
// The condition is false (result is true).
// Nothing is larger than m.
// Nothing is larger than m.
// m isn't the smallest.
// Now m is the smallest.

C# 語言規格

如需詳細資訊,請參閱<C# 語言規格>。語言規格是 C# 語法及用法的限定來源。

請參閱

參考

C# 關鍵字

?: 運算子 (C# 參考)

if-else 陳述式 (C++)

switch (C# 參考)

概念

C# 程式設計手冊

其他資源

C# 參考