if-else (Справочник по C#)
bool condition = true; if (condition) { Console.WriteLine("The variable is set to true."); } else { Console.WriteLine("The variable is set to false."); }
Можно выполнить примеры в этом разделе, размещая их в методе Main приложения консоли.
Оператор 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.
then-statement и else-statement могут состоять из одной или нескольких выписки выписки, заключенных в фигурные скобки ({}). Для одного оператора в теле конструкции скобки необязательны, но рекоммендуются.
// 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), можно указать, что ассоциация с помощью фигурных скобок для задания начало и конец вложенных выписки 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.
Пример
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.
// 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. }
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.