语句 if else

Posted DG - SheNg1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了语句 if else相关的知识,希望对你有一定的参考价值。

语句

        语句是指程序命令,都是按照顺序执行的。语句在程序中的执行顺序称为“控制流”或“执行流”。 根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同。

语句间的标点符号必须是英文标点,语句的结束标点是分号“;”。

语句的类型包括声明语句,表达式语句选择语句(分支)循环语句,跳转语句,异常语句

1、声明语句引:入新的变量或常量。变量声明可以选择为变量赋值。在常量声明中必须赋值。

例:

Int a=0;//声明变量a并赋值,也可以不赋值。

double d;

2、表达式语句:用于计算值的表达式语句必须在变量中存储该值。

例:

Sum=i+j;

Int x=a+b;

3、选择语句:ifelseswitchcase

4、循环语句:doforforeachwhile

5、跳转语句:break,continue,default,return

6、异常语句:try-catch-finally

一、选择语句:

Ifelse

If如果的意思,else另外(其他的所有条件)的意思

(1)

if(表达式) //表达式返回值是TrueFalse

{

}

说明:1.表达式返回的是bool值;2.小括号和花括号后面不需要加分号。

2

if(表达式)

{

}

else

{

}

 

3If的嵌套

if(表达式)

{

if()

{

}

    else

{

}

}

else

{

if()

{

}

}

 

4各种情况只能走其中之一,若上面的都没走,将执行else里面的。

 

if(表达式)

{

}

else if

{

}

else if

{

}

...

else

{

}

例1:输入三个整数,xyz,最终以从小到大的方式输出。利用嵌套。

            Console.Write("请输入x=");

            double x = double.Parse(Console.ReadLine());

            Console.Write("请输入y=");

            double y = double.Parse(Console.ReadLine());

            Console.Write("请输入z=");

            double z = double.Parse(Console.ReadLine());

            if (x < y && x < z)

            {

                if (y < z)

                {

                    //Console.WriteLine("从小到大排列为x="+x+"y="+y+"z="+z);

                    Console.WriteLine("从小到大排列为" + x + y + z);

 

                }

                else

                {

                    Console.WriteLine("从小到大排列为" + x + z + y);

                }

            }

            else if (y < x && y < z)

            {

                if (x < z)

                {

                    Console.WriteLine("从小到大排列为" + y + x + z);

                }

                else

                {

                    Console.WriteLine("从小到大排列为" + y + z + x);

                }

            }

            else if (z < x && z < y)

            {

                if (x < y)

                {

                    Console.WriteLine("从小到大的排列为" + z + x + y);

                }

                else

                {

                    Console.WriteLine("从小到大的排列为" + z + y + x);

                }

            }

            else

            {

                Console.WriteLine("输入错误!");

            }

            Console.ReadLine();

例2:输入年、月、日,判断时间日期格式是否正确
        年:0~9999
        月:1~12
        日:1.   1  3  5  7  8  10  12        31天
              2.   4  6  9  11                     30天
              3.   2      (1)闰年:29天    (2)平年:28天
            能被4整除却不能被100整除的年份。a%4==0&&a%100!=0 世纪年份能被400整除的是闰年a%400==0
            Console.Write("请输入年份:");
            int year = int.Parse(Console.ReadLine());
            if (year >= 0 && year <= 9999)
            {
                Console.Write("请输入月份:");
                int month = int.Parse(Console.ReadLine());
                if (month >= 1 && month <= 12)
                {
                    Console.Write("请输入日:");
                    int day = int.Parse(Console.ReadLine());
                    if (day >= 1 && day <= 31)
                    {
                        //31天的月份
                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                            Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);
                        }
                        else //4,6,9,11,2
                        {
                            if (month == 4 || month == 6 || month == 9 || month == 11)
                            {
                                if (day <= 30)
                                {
                                    Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);
                                }
                                else
                                {
                                    Console.WriteLine("输入有误!");
                                }
                            }
                            else//2
                            {
                                if (day <= 28)
                                {
                                    Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);
                                }
                                else//29,30,31
                                {
                                    if (day == 29)
                                    {
                                        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                                        {
                                            Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);
                                        }
                                        else
                                        {
                                            Console.WriteLine("输入有误!");
                                        }
                                    }
                                    else//30,31
                                    {
                                        Console.WriteLine("输入有误!");
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("输入的日期有误!");
                    }
                }
                else
                {
                    Console.WriteLine("输入的月份有误!");
                }
            }
            else
            {
                Console.WriteLine("输入的年份有误!");
            }
            Console.ReadLine();

以上是关于语句 if else的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句中能否含有if....else...判断语句?

if...else if...else和switch语句的注意点,以及和js的if...else if...else的不同

if else语句,条件语句,执行的数序

Shell if else语句(详解版)

Linux中if-else条件判断语句

Shell if else语句