已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Ch5SelectionStatements,ProgrammingConstruct,1)Wedividethebasicprogrammingconstructintothreetypes:SequentialStructure、SelectionStructureandLoopStructure.2)FlowChart(流程图):isagraphicalrepresentationinwhichspecificsymbolsandnecessarylettersareusedtodescribethesteps.3)Theflowchartaboutthethreebasicprogrammingconstructisasfollows:,Sequentialstructure,SelectionStructure,SelectionStructure,1、relationoperatorandrelationexpression,2、logicaloperatorsandlogicalexpression,3、ifstatements,4、switchstatements,5、examples,ChapterOutline,Threeforms:,(1)if(expression)statements(2)if(expression)statements1elsestatements2(3)if(expression1)statements1elseif(expression2)statements2elseif(expression3)statements3elseif(expressionm)statementsmelsestatementsn,ex,ex,ex,BriefSummary,Forme1:if(expression)statements,F(0),T(not0),ex:if(xy)printf(“%d”,x);Firstly,solvetheexpression.Ifthevalueoftheexpressionisnotzero,thenthecorrespondingststementsequenceisexcuted.Ifnoneoftheexpressionisfalse,thenexcutethestatementsafterifderectly.,example,ex:if(xy)printf(“%d”,x);elseprintf(“%d”,y);,Form2:if(expression)statements1elsestatements2,example,ex:if(number500)cost=0.15;elseif(number300)cost=0.10;elseif(number100)cost=0.075;elseif(number50)cost=0.05;elsecost=0;,Form3:nested-ifstatements,example,(1)Theexpressionofthreeformsbehindif-statementsalwaysislogicalorrelationexpression.,DCLofthreeif-statementsforms:,(2)Inform2andforme3,thereisasemicoloninfrontofelse,alsointheendofallstatements.,(3)Behindiforelse,itmayincludeonlyoneoperatestatements,alsoincludemanystatements,butshouldbedrawedtogetherwithbrace.(),Ex:#includemain()intx,y;scanf(“%d,%d”,CompileError!,ex:if(a+ba,Notice:Thebrace(“”)souterfaceneednotsemicolon。Becauseinthebrace()thereisafullcompoundstatement.,originalprogram:main()floata,b,t;scanf(“%f,%f”,ex1:Inputtwerealnumbers,pleaseoutputtherenumbersaccordingtotheorderoflittletobig.,Needthreefloatvariables:a,b,t,ex2:Inputthreerealnumbers,pleaseoutputtherenumbersaccordingtotheorderoflittletobig.,originalprogram:main()floata,b,c,t;scanf(“%f,%f,%f”,Return,#includemain()inta,b;printf(Enterintegera:);scanf(%d,ex3:Iputtwonumbers,decidethemtobeequallyornot.,Result:Enterintegera:12Enterintegerb:12a=b,Result:Enterintegera:12Enterintegerb:9a!=b,Return,Generalform:expression1?expression2:expression3,Conditionoperatorcommandsthreeoperateobjects,itisaoperatoralonelyinClanguage.,ex:max=(ab)?a:b;,Equalto:if(ab)max=a;elsemax=b;,Conditionoperator,DCL:(1)Executionorder:Firstly,solvetheexpression1,,Ifthevalueisture,thensolvetheexpression2,thismomenthereisthevalueofwholeexpressions.,Ifthevalueisfalse,thensolvetheexpression3,thismomenthereisthevalueofwholeexpressions.,ex:max=(ab)?a:b;,Ifa=3,b=4;,max=4;,Ifa=6,b=4;,max=6;,(2)Conditionoperatorovermatchassignedoperator,butunderrelationandarithmeticoperator.,(3)Conditionoperatorscombineddirecionisfromrighttoleft.,ex1:max=(ab)?a:b;,max=ab?a:b;,ex2:max=ab?a:b+1;,max=ab?a:(b+1);,ex3:max=ab?a:cd?c:d;,max=ab?a:(cd?c:d);,Supposea=1,b=2,c=3,d=4,(4)Conditionoperatorcanntreplceordinaryif-statements,onlywhentheembedstatementsinitisassignedstatements(andtwobranchesassigntothesamevariable).,ex:if(ab)printf(“%d”,a);elseprintf(“%d”,b);,printf(“%d”,ab?a:b);,(5)Thetypeofexpression1andexpression2canbesame,alsobedifferent.,ex:intx;x?a:b;,ex:xy?1:1.5;,Ex5:Inputaletter,iudgeitisacapitalletterornot,ifis,thenchangeittolower-caseletter;ifnot,thennothingchanged。Finally,outputthisletter。,main()charch;scanf(“%c“,Return,main()floatx;inty;scanf(“%f”,Return,Algorithm2:inputxifx0y=1outputy,Algorithm3:inputxifx=0)if(x0)y=1;elsey=0;elsey=-1;,p3:modifyto:y=-1;if(x!=0)if(x0)y=1;elsey=0;,p4:modifyto:y=0;if(x=0)if(x0)y=1;elsey=-1;,Return,#includemain()intx,y;print
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 空调安装工劳务协议书
- 申请解除房贷合同范本
- 活动承包举办合同范本
- 物流运输企业合同范本
- 渔船钢材收售合同范本
- 砖工劳务派遣合同范本
- 特殊行业引进合同范本
- 石场安全开采合同范本
- 电子消费卡订购协议书
- 安全领导力框架讲解
- 2025年北京事业单位招聘考试(水务基本常识)历年参考题库含答案详解(5套)
- 2025年工行招聘笔试真题及答案
- 租赁公司员工绩效规范规定
- 2025广西公需科目培训考试答案(90分)一区两地一园一通道建设人工智能时代的机遇与挑战
- 公路隧道火灾:成因、危害分析与科学救灾预案构建
- 摩托车研发项目管理办法
- 安全检查检查制度
- 生产安全生产事故案例
- 电气线路的安全技术课件
- 电化学储能电站设计标准
- 2025护理教学计划
评论
0/150
提交评论