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 > 10 和 n > 20 计算结果为 true,Result1 显示。如果 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");
}

在条件 (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 语句块。该示例嵌套在两内的 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# 编程指南

C# 关键字

?: 运算符(C# 参考)

if-else 语句 (C++)

switch(C# 参考)