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

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

语句可以嵌套,可以是以分号结尾的单行代码,也可以是语句块中的单行语句。语句块括在括号 {} 中,并且可以包含嵌套块。

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

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

例如:

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

double d;

“//”表示注释一行,“/*…*/”可以注释一段区域,注释后的内容变绿。

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

例如:

sum = i + j;//变量i和j在此之前必须先赋值。而且sum也需要声明类型。

int x = a + b; //或者在声明的同时进行运算。

3、选择语句:if, else, switch, case

4、循环语句:do, for, foreach, while

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

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

一、选择语句

(一)

if(表达式) //表达式返回值是True或False

{

}

说明:●表达式返回的是bool值;

●小括号和花括号后面不需要加分号。

例:int a = 15;

bool b = a < 10;

if (b)//条件,返回true或false

{

Console.WriteLine("a是10以内的数!");

}

Console.ReadLine();

(二)

if(表达式)

{

}

else

{

}

注:●else表示跟if的条件完全相反

●如果if没有执行,else就必须执行,如果if执行了,else就一定不执行

例:1.输入年龄,大于等于18显示成年,否则显示未成年。

Console.Write("请输入您的年龄:");

int age = int.Parse(Console.ReadLine());

if (age >= 18)

{

Console.WriteLine("成年");

}

else//另外的其他的所有条件  age<18

{

Console.WriteLine("未成年");

}

Console.ReadLine();

2.你能跑过豹子么?接收能或者不能。

第一种:

Console.Write("你能跑过豹子么?");

string ss = Console.ReadLine();

if (ss == "能")

{

Console.WriteLine("你比禽兽还禽兽!");

}

else if (ss == "不能")

{

Console.WriteLine("你连禽兽都不如!");

}

else

{

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

}

第二种:

Console.Write("你能跑过豹子么?");

string ss = Console.ReadLine();

if (ss == "能" || ss == "不能")

{

if (ss == "能")

{

Console.WriteLine("你比禽兽还禽兽!");

}

else

{

Console.WriteLine("你连禽兽都不如!");

}

}

else

{

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

}

(三)

if(表达式)

{

}

else if

{

}

else if

{

}

...

else

{

}

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

例:        Console.Write("请输入您的性别:");

string sex = Console.ReadLine();

if (sex == "男")

{

Console.WriteLine("您是男性!");

}

else if (sex == "女")

{

Console.WriteLine("您是女性!");

}

else//sex!="男"   sex!="女"

{

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

}

Console.ReadLine();

(四)

if(表达式)

{

if(){}

else{}

}

else

{

if(){}

}

if嵌套

例:        Console.Write("请输入您的年龄:");

int age = int.Parse(Console.ReadLine());

if (age >= 0 && age <= 135)//人的正常年龄范围

{

if (age <= 12)

{

Console.WriteLine("你是儿童!");

}

else if (age <= 18)

{

Console.WriteLine("你是青少年!");

}

else if (age <= 35)

{

Console.WriteLine("你是青年!");

}

else if (age <= 60)

{

Console.WriteLine("你是中年!");

}

else

{

Console.WriteLine("你是老年!");

}

}

else//不属于正常人的年龄范围

{

Console.WriteLine("你是属王八的么?");

}

Console.ReadLine();

注:错误!!!!!!!!!

if()

{}

if(){}

else{}

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

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

int a = int.Parse(Console.ReadLine());

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

int b = int.Parse(Console.ReadLine());

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

int c = int.Parse(Console.ReadLine());

if (a < b && a < c )

{

if(b<c)

{

Console.WriteLine("三个数由小到大为" + a + b + c);

}

else

{

Console.WriteLine("三个数由小到大为" +a+c+b);

}

}

else if(b<a&&b<c)

{

if(a<c)

{

Console.WriteLine("三个数由小到大为" + b + a + c);

}

else

{

Console.WriteLine("三个数由小到大为" + b + c +a);

}

}

else//c是最小的

{

if (a < b)

{

Console.WriteLine("三个数由小到大为" + c + a + b);

}

else

{

Console.WriteLine("三个数由小到大为" + c + b + a);

}

}

2、输入学生姓名,输入考试成绩。若是100,【恭喜你***,满分通过!】若是大于等于80小于100,【**,你很优秀,继续保持!】若是大于等于60小于80,【**成绩良好】大于等于50小于60,【**就差一点点,下次一定要至少及格!】小于50,【**你是笨蛋么?】

Console.WriteLine("请输入您的姓名");

string a = Console.ReadLine();

Console.WriteLine("请输入你的考试成绩");

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

if (b >= 0 && b <= 100)

{

if (b == 100)

{

Console.WriteLine("恭喜你" + a + ",满分通过");

Console.ReadLine();

}

else if (b < 100 && b >= 80)

{

Console.WriteLine(a + ",你很优秀,继续保持");

Console.ReadLine();

}

else if (b < 80 && b >= 60)

{

Console.WriteLine(a + "成绩良好");

Console.ReadLine();

}

else if (b < 60 && b >= 50)

{

Console.WriteLine(a + "就差那么一点点,下次一定要及格");

Console.ReadLine();

}

else//b<50

{

Console.WriteLine(a + "你是笨蛋吗?");

Console.ReadLine();

}

}

else

{

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

}

3、有一组函数:y = x (x<1);y = 2x -1  (1<=x<10);y = 3x-11  (x>=10)。括号内是x的满足条件。实现功能,随意输入一个x值,输出y的值。

Console.WriteLine("请输入一个x的值:");

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

if (x < 1)

{

Console.WriteLine(x);

}

else

{

if (x>=1&&x<10)

{

Console.WriteLine(2*x-1);

}

else

{

if (x>=10)

{

Console.WriteLine(3*x-11);

}

}

Console.ReadLine ();

4、输入整数a和b,若a2+b2大于100,则输出a2+b2的和,否则输出两数之和

Console.Write("请输入整数a:");

int a = int.Parse(Console.ReadLine());

Console.Write("请输入整数b:");

int b = int.Parse(Console.ReadLine());

if ((a * a + b * b) > 100)

{

Console.WriteLine(a * a + b * b);

}

else//a*a+b*b<=100

{

Console.WriteLine(a + b);

}

Console.ReadLine();

5、相亲过程:你有房子么?你有钱么?你有能力么?【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】利用if嵌套做相亲过程

Console.Write("你有房子么?");

string h = Console.ReadLine();

if (h == "有")

{

Console.WriteLine("结婚吧");

}

else

{

Console.Write("你有钱么");

string j = Console.ReadLine();

if (j == "有")

{

Console.WriteLine("先买房子再结婚");

}

else

{

Console.Write("你有能力么");

string k = Console.ReadLine();

if (k == "有")

{

Console.WriteLine("先赚钱再买房子再结婚");

}

else

{

Console.WriteLine("拜拜~~");

}

}

}

Console.ReadLine();

6、输入一个年份,判断是否是闰年(能被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)

{

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

{

Console.WriteLine("您输入的年份是闰年!您输入的年份是:{0}", year);

}

else

{

Console.WriteLine("您输入的年份是平年!您输入的年份是:"+year);

}

}

else

{

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

}

Console.ReadLine();

7、输入年、月、日,判断时间日期格式是否正确:

●年: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)

{

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

{

Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);

}

else

{

if (month == 4 || month == 6 || month == 9 || month == 11)

{

if (day <= 30)

{

Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);

}

else

{

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

}

}

else

{

if (day <= 28)

{

Console.WriteLine("输入的日期格式正确!您输入的日期为:{0}-{1}-{2}", year, month, day);

}

else

{

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

{

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

}

}

}

}

}

else

{

Console.WriteLine("输入的日期有误!");

}

}

else

{

Console.WriteLine("输入的月份有误!");

}

}

else

{

Console.WriteLine("输入的年份有误!");

}

Console.ReadLine();

8、ax*x+bx+c=0判断方程是否是一元二次方程,和根的情况。根据公式判断方程的根的状况,公式的值大于零有两个根,等于零有一个根,小于零没有根。(首先明白什么是一元二次方程,如果a等于0,方程式不是一元二次方程。用公式:代尔塔△=b2-4*a*c判断根的情况:△<0则方程无解,△=0方程有两个相等的实根,△>0方程有两个不同的实根。求解则用到另一个公式:x=(-b±√b2-4ac)/(2*a)根号√需要用到函数 Math.Sqrt())

Console.WriteLine("求方程式ax*x+bx+c=0");

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

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

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

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

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

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

double de = b * b - 4 * a * c;

if (a == 0)//如果a是0,则不是一元二次方程

{

Console.WriteLine("不是一元二次方程");

}

else

{

Console.WriteLine("是一元二次方程");

if (de >= 0)

{

double x1 = (-b + Math.Sqrt(de)) / (2 * a);

double x2 = (-b - Math.Sqrt(de)) / (2 * a);

if (de > 0)

{

Console.WriteLine("方程式有两个不同的实根");

Console.WriteLine("x1=" + x1.ToString() + "x2=" + x2.ToString());

}

else

{

Console.WriteLine("方程式有两个相同的实根");

Console.WriteLine("x1=x2=" + x1.ToString());

}

}

else if (de < 0)

{

Console.Write("方程式没有实根");

}

}

Console.ReadLine();//防止控制台闪退

C#语句1:选择语句一(if else )的更多相关文章

  1. JAVA-三大语句(选择语句、条件语句、循环语句)

    跳出指定的for循环体,和goto很像 1 K:for(int i=0;i<3;i++){//给这个for循环体取一个名字为K 2 for(int j=0;j<3;j++){ 3 if(j ...

  2. 语句:分支语句、switch case ——7月22日

    语句的类型包括:声明语句.表达式语句.选择语句.循环语句.跳转语句.异常语句 1.声明语句引:入新的变量或常量. 变量声明可以选择为变量赋值. 在常量声明中必须赋值. 例如: int i = 0;// ...

  3. java基础2_运算符,选择语句

    算数运算符  +    相加  字符串的连接  正数 -    相减  负数 *    相乘 /    相除    10 / 3 ==> 3  两个操作数中精度最高的是int 结果也是int % ...

  4. C#中的选择语句

    一.选择语句 if,else if是如果的意思,else是另外的意思,if'后面跟()括号内为判断条件,如果符合条件则进入if语句执行命令.如果不符合则不进入if语句.else后不用加条件,但是必须与 ...

  5. shell选择语句

    if语句 1) if ... else 语句 if ... else 语句的语法: if [ expression ] then Statement(s) to be executed if expr ...

  6. go语言选择语句 switch case

    根据传入条件的不同,选择语句会执行不同的语句.下面的例子根据传入的整型变量i的不同而打印不同的内容: switch i { case 0: fmt.Printf("0") case ...

  7. Java语法基础(三)----选择结构的if语句、switch语句

    [前言] 流程控制语句: 在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说程序的流程对运行结果有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过 ...

  8. 显示刚刚添加的最后一条数据,access,选择语句,select

    显示刚刚添加的最后一条数据,access,选择语句,select select top 1  * from s1 order by id desc

  9. MSSQLServer基础06(变量,case,选择语句)

    变量 声明:declare @UserName nvarchar(50) 赋值1:set @UserName=N'杨':修改 赋值2:select @UserName=N'牛':修改 输出:print ...

随机推荐

  1. C#中考虑为大对象使用弱引用

    1.无论怎样尽力,我们总是会使用到某些需要大量内存的数据,而这些内存并不需要经常访问.或许你需要从一个大文件中查找某个特定的值,或者算法需要一个较大的查询表.这时,你也许会采用2中不太好做法:第一种是 ...

  2. Android使用SQLite数据库(1)

    Android中使用SQLite数据库要通过SQLiteOpenHelper类. 首先,定义相关变量: // 数据库变量 DatabaseHelper mDBH; SQLiteDatabase db; ...

  3. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  4. 酷!使用 jQuery & Canvas 制作相机快门效果

    在今天的教程中,我们将使用 HTML5 的 Canvas 元素来创建一个简单的摄影作品集,它显示了一组精选照片与相机快门的效果.此功能会以一个简单的 jQuery 插件形式使用,你可以很容易地整合到任 ...

  5. HubSpot – 网站开发必备的 jQuery 信息提示库

    HubSpot 一款功能丰富的 jQuery 消息提示插件.它可以帮助你个性化显示您的应用程序的事务性消息.您可以轻松地包裹 Ajax 请求进度,成功和错误消息,还可以添加操作链接到您的消息中. Hu ...

  6. Apple Watch PSD 源文件【免费素材下载】

    Apple Watch 是苹果公司于2014年9月发布的一款智能手表.分为运动款.普通款和定制款三种,采用蓝宝石屏幕,有银色,金色,红色,绿色和白色等多种颜色可以选择.这里分享的是 Apple Wat ...

  7. 【再探backbone 02】集合-Collection

    前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...

  8. 关于SAP的事务提交和回滚(LUW)

    1 Sap的更新的类型 在sap中,可以使用CALL FUNCTION ... IN UPDATE TASK将多个数据更新绑定到一个database LUW中.程序使用COMMIT WORK提交修改请 ...

  9. Atitit。Web server Jetty9 使用 attilax 总结

    Atitit.Web server Jetty9 使用 attilax 总结 1.1. 静态文件的资源1 1.2. Servlet使用1 1.3. code1 1.1. 静态文件的资源 WebAppC ...

  10. Warning: Attempt to present on whose view is not in the window hierarchy!

    当我想从一个VC跳转到另一个VC的时候,一般会用 - (void)presentViewController:(UIViewController *)viewControllerToPresent a ...