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# 语法和用法的权威资料。