zoukankan      html  css  js  c++  java
  • C#中if语句的基本运用

      一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。

    如果布尔表达式为 true,则 if 语句内的代码块将被执行。如果布尔表达式为 false,则 if 语句结束后的第一组代码(闭括号后)将被执行。

       
                int a = 10;
    
                /* 使用 if 语句检查布尔条件 */
                if (a < 20)
                {
                    /* 如果条件为真,则输出下面的语句 */
                    Console.WriteLine("a 小于 20");
                }
                Console.WriteLine("a 的值是 {0}", a);
                Console.ReadLine();
    

      

    if...else if...else 语句

    一个 if 语句后可跟一个可选的 else if...else 语句,这可用于执行多种条件。

    当使用 if...else if...else 语句时,以下几点需要注意:

    • 一个 if 后可跟零个或一个 else,它必须在任何一个 else if 之后。
    • 一个 if 后可跟零个或多个 else if,它们必须在 else 之前。
    • 一旦某个 else if 匹配成功,其他的 else if 或 else 将不会被执行
    if(boolean_expression 1)
    {
       /* 当布尔表达式 1 为真时执行 */
    }
    else if( boolean_expression 2)
    {
       /* 当布尔表达式 2 为真时执行 */
    }
    else if( boolean_expression 3)
    {
       /* 当布尔表达式 3 为真时执行 */
    }
    else 
    {
       /* 当上面条件都不为真时执行 */
    }
    

      下面为实例:

       /* 局部变量定义 */
                int a = 100;
    
                /* 检查布尔条件 */
                if (a == 10)
                {
                    /* 如果 if 条件为真,则输出下面的语句 */
                    Console.WriteLine("a 的值是 10");
                }
                else if (a == 20)
                {
                    /* 如果 else if 条件为真,则输出下面的语句 */
                    Console.WriteLine("a 的值是 20");
                }
                else if (a == 30)
                {
                    /* 如果 else if 条件为真,则输出下面的语句 */
                    Console.WriteLine("a 的值是 30");
                }
                else
                {
                    /* 如果上面条件都不为真,则输出下面的语句 */
                    Console.WriteLine("没有匹配的值");
                }
                Console.WriteLine("a 的准确值是 {0}", a);
                Console.ReadLine();
    

      

  • 相关阅读:
    GridView使用技巧
    ilspy反编译
    Editplus php
    SQL 日期相减(间隔)datediff函数
    cmd创建文件命令
    iis7 bug解决
    删除qq互联
    discuz 数据库文件密码修改
    linux zip命令
    asp.net调用js方法
  • 原文地址:https://www.cnblogs.com/jiulinghoudabai/p/6476995.html
Copyright © 2011-2022 走看看