<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 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."); } ``` 可以本主題中的示例通過將它們負責在控件個 app 的 **Main** 方法。 如下面的示例所示,在 c# 中為 **if** 語句可以采用兩種形式。 ``` // 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** 語句,則為;condition 計算結果為 true,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**。在下面的示例中,則為;m &gt; 10 和 n &gt; 20 計算結果為 true,Result1 顯示。如果 m &gt; 10 為 true,但 n &gt; 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 &gt; 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"); } ``` 在條件 (m &gt; 10) 計算為 FALSE,Result2 顯示。 在下面的示例中,將從鍵盤輸入字符,因此,程序使用嵌套的 **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. ``` 正如在另一個語句塊或塊可以是任何有效的語句,可以為該條件使用任何有效的布爾值表示樣式。您可以使用邏輯運算符 (例如 [&&](https://msdn.microsoft.com/zh-CN/library/2a723cdk.aspx),[&](https://msdn.microsoft.com/zh-CN/library/sbf85k1c.aspx),[||](https://msdn.microsoft.com/zh-CN/library/6373h346.aspx),[|](https://msdn.microsoft.com/zh-CN/library/kxszd0kx.aspx)并使多個條件的 [!](https://msdn.microsoft.com/zh-CN/library/f2kd6eb2.aspx)。下面的代碼演示示例。 ``` // 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# 語言規范](https://msdn.microsoft.com/zh-CN/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [C# 參考](https://msdn.microsoft.com/zh-CN/library/618ayhy6.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-CN/library/67ef8sbd.aspx) [C# 關鍵字](https://msdn.microsoft.com/zh-CN/library/x53a06bb.aspx) [?: 運算符(C# 參考)](https://msdn.microsoft.com/zh-CN/library/ty67wk28.aspx) [if-else 語句 (C++)](https://msdn.microsoft.com/zh-CN/library/y34a3dk2.aspx) [switch(C# 參考)](https://msdn.microsoft.com/zh-CN/library/06tc147t.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看