zoukankan      html  css  js  c++  java
  • C# if语句

    一、C# if语句

    if语句根据条件判断代码该执行哪一个分支。

    if语句有两个或两个以上的分支供代码选择,但是每次只能执行一个分支。

    1. 基本if语句

    语法格式如下:

    if(expression)
    {
        statement;
    }或

    if(expression)
    {
        statement1;
    }
    else
    {
        statement2;
    }

    如果expression的值为true,就执行statement1;否则,执行statement2。

    如果省略else子句,那么当expression的值为false时,就不会执行任何语句。

    statement语句可以是一条语句或多条语句。一条语句时可以省略花括号,多条语句时必须使用花括号。

    2. 使用else if的if语句

    语法格式如下:

    if(expression1)
    {
        statement1;
    }
    else if(expression2)
    {
        statement2;
    }
    ......
    else
    {
        statementn;
    }

    可以添加多个else if子句。

    如果expression1的值为true,则执行statement1,然后跳过else if子句和else子句,直接执行后面的语句。

    如果expression1的值为false,就判断expression2,如果为true,就执行statement2;如果为false,就断续判断后面的expression语句,以此类推……,如果所有的expression的值都为false,就执行else子句中的statementn语句。

    3. 使用嵌套结构的if语句

    语法格式如下:

    if(expression1)
    {
        ......
        statement1;
        if(expression2)
        {
            ......
            statement2;
            ......
        }
        else
        {
            ......
            statement3;
            ......
        }
        ......
    }
    else
    {
        if(expression4)
        {
            ......
            statement4;
            ......
        }
        statementn;
        ......
    }

    每一个statement语句的前面和后面都可以添加if语句或者if else语句。

    对于多层if嵌套结构,必须注意:每一个else都必须与一个离它最近的if相搭配。

    二、提示

    if语句中的表达式必须是布尔类型(bool)表达式,它的值为true(真)或false(假)。

    expression表达式中可能出现的运算符有:

    ?==:表示当两个值相等时,该表达式的值为true。
    ?!=:表示当两个值不相等时,该表达式的值为true。
    ?<、<=、>、>=:分别表示当两个值满足小于、小于或等于、大于、大于或等于关系时,该表达式的值为true。
    ?&&:表示当“&&”两端的表达式的值都为true时,该表达式的值为true。
    ?||:表示当“||”两端的表达式有一个为true时,该值为true。

    三、示例

    示例一

    下面是if语句代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                // C# if语句-www.baike369.com
                int x = 68;
                if (x >= 60)
                {
                    Console.WriteLine("及格");
                }
                Console.ReadLine();
            }
        }
    }

    运行结果:

    及格

    示例二

    下面是if else语句代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x = 55;
                if (x >= 60)
                {
                    Console.WriteLine("及格");
                }
                else
                {
                    Console.WriteLine("不及格");
                }
                Console.ReadLine();
            }
        }
    }

    运行结果:

    不及格

    示例三

    下面是使用else if子句的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x = 75;
                if (x == 100)
                {
                    Console.WriteLine("满分");
                }
                else if (x < 100 && x >= 90)
                {
                    Console.WriteLine("优秀");
                }
                else if (x < 90 && x >= 80)
                {
                    Console.WriteLine("良好");
                }
                else if (x < 80 && x >= 70)
                {
                    Console.WriteLine("较好");
                }
                else if (x < 70 && x >= 60)
                {
                    Console.WriteLine("及格");
                }
                else
                {
                    Console.WriteLine("不及格");
                }
                Console.ReadLine();
            }
        }
    }

    运行结果:

    较好

    示例四

    下面是使用嵌套结构的if语句代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                int x = 93;
                if (x >= 60)
                {
                    if (x == 100)  // 嵌套结构的if语句
                    {
                        Console.WriteLine("满分");
                    }
                    else if (x < 100 && x >= 90)
                    {
                        Console.WriteLine("优秀");
                    }
                    else if (x < 90 && x >= 80)
                    {
                        Console.WriteLine("良好");
                    }
                    else if (x < 80 && x >= 70)
                    {
                        Console.WriteLine("较好");
                    }
                    else if (x < 70 && x >= 60)
                    {
                        Console.WriteLine("及格");
                    }
                }
                else
                {
                    Console.WriteLine("不及格");
                }
                Console.ReadLine();
            }
        }
    }

    运行结果:

    优秀

  • 相关阅读:
    Codeforces 166E. Tetrahedron
    Codeforce 687A. NP-Hard Problem
    Codeforces 570C. Replacement
    Codeforces 554B. Ohana Cleans Up
    Codeforces 482A. Diverse Permutation
    Codeforces 431C. k-Tree
    Codeforces 750B. Spider Man
    Codeforces 463A. Caisa and Sugar
    Codeforces 701B. Cells Not Under Attack
    Codeforces 445A. DZY Loves Chessboard
  • 原文地址:https://www.cnblogs.com/melao2006/p/4239404.html
Copyright © 2011-2022 走看看