日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

if-else运用及技巧(C# 参考)

發布時間:2024/4/13 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 if-else运用及技巧(C# 参考) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?if-else

  • if 語句基于布爾表達式的值來識別運行哪個語句。 在下面的示例中, bool 變量 condition 已被設置為 true ,然后被簽入到了 if 語句。 輸出為 The variable is set to true.。
bool condition = true;if (condition) {Console.WriteLine("The variable is set to true."); } else {Console.WriteLine("The variable is set to false."); }
  • 你可以通過將本主題中的示例放入控制臺應用的 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 語句的 else-statement 和 if-else 永遠不能同時運行。 then-statement 或 else-statement 運行后,控件將轉移到 if 語句之后的下一個語句。
  • 在不包括 if 語句的 else 語句中,如果 condition 為 true,則 then-statement 將運行。 如果 condition 為 false,則控件將轉移到 if 語句之后的下一個語句。
  • then-statement 和 else-statement 都可由單個語句或包含在括號中 ({}) 的多個語句組成。 對于單個語句,括號是可選的,但建議選擇。
  • then-statement 和 else-statement 中的語句可為任何類型,包括嵌套在原始 if 語句中的另一個 if 語句。 在嵌套的 if 語句中,每個 else 子句都屬于上一個無相應 if 的 else。 在下面的示例中,如果 Result1 和 m > 10 計算結果都為 true,則將顯示 n > 20 。 如果 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 為 false 的時候顯示 (m > 10) ,則可以通過使用括號來指定此關聯,以建立嵌套的 if 語句的開頭和結尾,如以下示例所示。
// Try with m = 12 and then with m = 8. if (m > 10) {if (n > 20)Console.WriteLine("Result1"); } else {Console.WriteLine("Result2"); }
  • 如果條件 (m > 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 語句嵌套到 else 塊中,如以下部分代碼所示。 示例將 if 語句嵌套在兩個 else 塊和一個 then 塊中。 注釋指定每個塊中哪些條件為 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.
  • 正如 else 塊或 then 塊中的語句可以是任何有效的語句一樣,你可以將任何有效的布爾表達式用于此條件。 可使用 !、&&、||、&、| 和 ^ 等邏輯運算符來創建復合條件。 下面的代碼演示了示例。
// 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# 語法和用法的權威資料。

總結

以上是生活随笔為你收集整理的if-else运用及技巧(C# 参考)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。