版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Ch5SelectionStatementsProgrammingConstruct1)Wedividethebasicprogrammingconstructintothreetypes:SequentialStructure、SelectionStructureandLoopStructure.2)FlowChart(流程图):isagraphicalrepresentationinwhichspecificsymbolsandnecessarylettersareusedtodescribethesteps.3)Theflowchartaboutthethreebasicprogrammingconstructisasfollows:ABconditionABconditionASequentialstructureSelectionStructureSelectionStructure1、relationoperatorandrelationexpression
2、logicaloperatorsandlogicalexpression3、ifstatements4、switchstatements5、examplesChapterOutlineThreeforms:
(1)if(expression)statements
(2)if(expression)statements1
elsestatements2
(3)if(expression1)statements1elseif(expression2)
statements2elseif(expression3)
statements3
elseif(expressionm)statementsmelsestatementsnexexex第五章选择结构If语句的形式BriefSummary
Forme1:if(expression)statementsexpressionstatementsF(0)T(not0)ex:
if(x>y)printf(“%d”,x);Firstly,solvetheexpression.Ifthevalueoftheexpressionisnotzero,thenthecorrespondingststementsequenceisexcuted.Ifnoneoftheexpressionisfalse,thenexcutethestatementsafterifderectly.第五章选择结构If语句的形式exampleexpressionstatements1statements2TFex:if(x>y)printf(“%d”,x);elseprintf(“%d”,y);Form2:if(expression)statements1
elsestatements2第五章选择结构If语句的形式exampleexpression1expression2expression3expression4statements1statements2statements3statements4statements5FFFFTTTTex:if(number>500)cost=0.15;elseif(number>300)cost=0.10;elseif(number>100)cost=0.075;elseif(number>50)cost=0.05;elsecost=0;Form3:nested-ifstatements第五章选择结构If语句的形式example(1)Theexpressionofthreeformsbehindif-statementsalwaysislogicalorrelationexpression.DCLofthreeif-statementsforms:(2)Inform2andforme3,thereisasemicoloninfrontofelse,alsointheendofallstatements.(3)Behindiforelse,itmayincludeonlyoneoperatestatements,alsoincludemanystatements,butshouldbedrawedtogetherwithbrace.({})第五章选择结构If语句的形式Ex:#include<stdio.h>main(){intx,y;scanf(“%d,%d”,&x,&y);if(x>y)x=y;y=x;elsex++;y++;printf(“%d,%d\n”,x,y);}CompileError!ex: if(a+b<c&&b+c>a&&c+a>b) { s=0.5*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%6.sf”,area); } elseprintf(“itisnotatrilateral”);
Notice: Thebrace(“}”)’souterface
neednotsemicolon。Becauseinthebrace({})thereisafullcompoundstatement.第五章选择结构If语句的形式
originalprogram:main(){floata,b,t;scanf(“%f,%f”,&a,&b);if(a>b){t=a;a=b;b=t;}printf(“%5.2f,%5.2f”,a,b);}ex1:Inputtwerealnumbers,pleaseoutputtherenumbersaccordingtotheorderoflittletobig.Needthreefloatvariables:
a,b,t第五章选择结构If语句举例ex2:Input
three
real
numbers,please
output
there
numbers
according
to
the
order
of
little
to
big.originalprogram:main() {floata,b,c,t; scanf(“%f,%f,%f”,&a,&b,&c); if(a>b){t=a;a=b;b=t;} if(a>c){t=a;a=c;c=t;} if(b>c){t=b;b=c;c=t;} printf(“%5.2f,%5.2f,%5.2f”,a,b,c); }第五章选择结构If语句举例Return#include<stdio.h>main(){inta,b;printf("Enterintegera:");scanf("%d",&a);printf("Enterintegerb:");scanf("%d",&b);
if(a==b)printf("a==b\n");elseprintf("a!=b\n");}ex3:Iputtwonumbers,decidethemtobeequallyornot.Result:Enterintegera:12
Enterintegerb:12a==bResult:Enterintegera:12
Enterintegerb:9a!=b第五章选择结构If语句嵌套举例ReturnGeneralform: expression1?expression2:expression3Conditionoperatorcommandsthreeoperateobjects,itisaoperatoralonelyinClanguage.ex:max=(a>b)?a:b;Equalto:
if(a>b)max=a;elsemax=b;第五章选择结构条件运算符Conditionoperator
DCL:(1)Executionorder:Firstly,solvetheexpression1,
Ifthevalueisture,thensolvetheexpression2,thismomenthereisthevalueofwholeexpressions.Ifthevalueisfalse,thensolvetheexpression3,thismomenthereisthevalueofwholeexpressions.ex:max=(a>b)?a:b;Ifa=3,b=4;max=4;Ifa=6,b=4;max=6;第五章选择结构条件运算符(2)Conditionoperatorovermatchassignedoperator,butunderrelationandarithmeticoperator.(3)Conditionoperator’scombineddirecionisfromrighttoleft.ex1:max=(a>b)?a:b;max=a>b?a:b;ex2:max=a>b?a:b+1;max=a>b?a:(b+1);ex3:max=a>b?a:c>d?c:d;max=a>b?a:(c>d?c:d);Supposea=1,b=2,c=3,d=4第五章选择结构条件运算符(4)Conditionoperatorcann’treplceordinaryif-statements,onlywhentheembedstatementsinitisassignedstatements(andtwobranchesassigntothesamevariable).ex:
if(a>b)printf(“%d”,a);elseprintf(“%d”,b);printf(“%d”,a>b?a:b);(5)Thetypeofexpression1andexpression2canbesame,alsobedifferent.ex:
intx;x?‘a’:‘b’;ex:x>y?1:1.5;第五章选择结构条件运算符Ex5:Inputaletter,iudgeitisacapitalletterornot,ifis,thenchangeittolower-caseletter;ifnot,thennothingchanged。Finally,outputthisletter。main(){charch;scanf(“%c“,&ch);ch=(ch>='A‘&&ch<='Z‘)?(ch+32):ch;printf(“%c“,ch);}第五章选择结构条件运算符Returnex6:Function-1(x<0)0(x=0)1(x>0)y=Writeaprogramme,inputthevalueofx.Thenouputy.main(){floatx;inty;scanf(“%f”,&x);if(x<0)y=-1;if(x==0)y=0;if(x>0)y=1;printf(“\n%f,%d”,x,y);}第五章选择结构If语句嵌套举例ReturnAlgorithm2:inputxifx<0y=-1else:
ifx=0y=0ifx>0y=1outputyAlgorithm3:inputxifx<0y=-1else:
ifx=0y=0else:y=1ouputy第五章选择结构If语句嵌套举例p1:main(){floatx;inty;scanf(“%f”,&x);
if(x<0)y=-1;elseif(x==0)y=0;elsey=1;printf(“x=%f,y=%d\n”,x,y);}p2:modifyto:if(x>=0)if(x>0)y=1;elsey=0;elsey=-1;p3:modifyto:y=-1;if(x!=0)if(x>0)y=1;elsey
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年港口REITs“盘活-投资-提升-再盘活”良性循环机制
- 2026年深海采矿活动环境管理策略优化方案
- 济南历下区2025-2026学年初三下第七次模拟化学试题含解析
- 陕西省延安市名校2026届初三第一次月考-化学试题含解析
- 常州市重点中学2026年初三下学期“扬帆起航”生物试题含解析
- 2026届内蒙古鄂尔多斯康巴什新区达标名校初三下-半期考试生物试题试卷含解析
- 2026年湖南省永州市祁阳县初三考前适应性测试化学试题含解析
- 甘肃省广河县重点中学2026年初三生物试题开学统练试题含解析
- 2026届安徽省濉溪县联考初三下学期阶段性练习化学试题含解析
- 2026年江苏省南京市宁海五十中学初三4月考试题-生物试题试卷含解析
- 产品工业设计外观规范手册
- 安徽能源集团秋招面试题及答案
- 2026年沈阳职业技术学院单招职业技能测试模拟测试卷附答案解析
- 新安全生产法宣讲课件
- 2025年《三级公共营养师》考试练习题库及答案
- 法院安全保密教育培训课件
- 2026年及未来5年中国城市地铁综合监控系统市场运行态势及行业发展前景预测报告
- 干细胞治疗共济失调的联合用药策略
- 金融控股公司并表管理指引
- 食堂超龄用工协议书
- 眩晕培训课件
评论
0/150
提交评论