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-statementif-else 永远不能同时运行。 then-statementelse-statement 运行后,控件将转移到 if 语句之后的下一个语句。
  • 在不包括 if 语句的 else 语句中,如果 condition 为 true,则 then-statement 将运行。 如果 condition 为 false,则控件将转移到 if 语句之后的下一个语句。
  • then-statementelse-statement 都可由单个语句或包含在括号中 ({}) 的多个语句组成。 对于单个语句,括号是可选的,但建议选择。
  • then-statementelse-statement 中的语句可为任何类型,包括嵌套在原始 if 语句中的另一个 if 语句。 在嵌套的 if 语句中,每个 else 子句都属于上一个无相应 ifelse。 在下面的示例中,如果 Result1m > 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# 语法和用法的权威资料。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值