已阅读5页,还剩47页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.,Chapter5DecisionMakingandBranching,PROGRAMMINGINANSIC,.,Question,Questions:Howdowejudgewhetherastudentpassanexaminationaccordingtohisscore?Howdowedecidehisgradeaccordingtohisscore?Inhumannaturelanguage:If,thenInC:Decision-makingstatement(branchstatement),.,Chapter5,Inthischapter,wewilllearn:Decision-makingstatement:ifConditionaloperator:?:Multiwaydecision-makingstatement:switchAssistedcontrolstatement:break,.,3FormsofifStatement,Form1themostsimpleformif(testexpression)statement;,if(score=60)printf(Hepassedthisexamination!);,.,3FormsofifStatement,Form2themostgeneralformif(testexpression)statement1;elsestatement2;,if(score=60)printf(Hepassedthisexamination!);elseprintf(Hefailedinthisexamination!);,.,3FormsofifStatement,Form3thenestedformif(exp1)s1;elseif(exp2)s2;elseelseif(expn)sn;elses;,if(score=90)grade=A;elseif(score=80)grade=B;elseif(score=70)grade=C;elseif(score=60)grade=D;elsegrade=E;,.,ifStatement,Thevalueofthetestexpressionmaybeanytype.Ifitequalszero,itisfalse,otherwisetrue.,if(a=b,if(3)printf(OK);,OK,.,if(a=2)printf(%d,a);elseprintf(“Wrong);,if(a=0)printf(%d,a);elseprintf(“Wrong);,ifStatement,Thevalueofthetestexpressionmaybeanytype.Ifitequalszero,itisfalse,otherwisetrue.,2,Wrong,.,ifStatement,Thestatementfollowingiforelsemaybeasinglestatementoracompoundstatement.,main()intx,y;scanf(%d,%d,CompileError!,Error6:Misplacedelseinfunctionmain,.,ifStatementProgram1,Input2realnumbers,andoutputtheminascendingorder.Step1:Read2realnumbersintovariablexandy.Step2:Ifxisgreaterthany,exchangethem.Step3:Outputxandy.,.,ifStatementProgram1,main()floatx,y,temp;printf(Pleaseinput2numbers:n);scanf(%f%f,Pleaseinput2numbers:13.22.1The2numbersare:2.10,13.20,.,ifStatementProgram1,Input2realnumbers,andoutputtheminascendingorder.Step1:Read2realnumbersintovariablexandy.Step2:Ifxislessthany,outputxandy,orelseoutputyandx.,.,ifStatementProgram1,main()floatx,y;printf(Pleaseinput2numbers:n);scanf(%f%f,.,ifStatementProgram1,ProgrammingExercises:Input3realnumbers,andoutputtheminascendingorder.(Homework!),.,ifStatementProgram2,Readinoneyear,andjudgewhetheritisaleapyearornot.Step1:Readintheyear.Step2:Ifthetestexpression(year%4=0printf(Pleaseinputtheyear:n);scanf(%d,Pleaseinputtheyear:19001900isnotaleapyear!,.,ifStatementProgram2,Step1:Settheflagvariableisleapas0.Step2:Readintheyear.Step3:Ifthetestexpression(year%4=0printf(Pleaseinputtheyear:n);scanf(%d,Pleaseinputtheyear:20002000isaleapyear!,.,ifStatementProgram3,Readinonecharacter,andjudgewhichkindofcharacteritis:afigure,oraletter,orother.Step1:Readthecharacterintovariablech.Step2:If(ch=0elsestatement2;elseif(exp3)statement3;elsestatement4;,.,NestingofifelseStatement,“else”matchingprinciple:Anelseisalwayslinkedtotheclosestnon-terminatedif.,if(exp1)if(exp2)statement1;elsestatement2;,if(exp1)if(exp2)statement1;elsestatement2;,.,ifStatementProgram4,Accordingtothescore,decidethegrade.score=80:gradeA;score=60,.,ifStatementProgram4,main()floatscore;printf(Pleaseinputthescore:n);scanf(%f,Pleaseinputthescore:75.5ThegradeisB!,.,ifStatementProgram4,main()floatscore;chargrade;printf(Pleaseinputthescore:n);scanf(%f,.,ifStatementProgram4,main()floatscore;chargrade;printf(Pleaseinputthescore:n);scanf(%f,.,ifStatementProgram4,main()floatscore;chargrade;printf(Pleaseinputthescore:n);scanf(%f,.,ConditionalOperator?:,Ternaryoperator:conditionalexp?exp1:exp2,.,ConditionalOperator?:,Ternaryoperator:conditionalexp?exp1:exp2,if(ab)max=a;elsemax=b;,max=ab?a:b;,if(ab)printf(%d,a);elseprintf(%d,b);,printf(%d,ab?a:b);,.,ConditionalOperator?:,Ternaryoperator:conditionalexp?exp1:exp2Howtooutputthevalueofa+|b|?,if(b0)printf(%f,a+b);elseprintf(%f,ab);,printf(%f,b0?a+b:ab);,printf(%f,a+(b0?b:-b);,.,ConditionalOperator?:,Ternaryoperator:conditionalexp?exp1:exp2Precedence:higherthanassignmentoperatorslowerthan|Associativity:RighttoleftConditionalexpressioncanbenested.x0?1:xy?1:1.5,.,ConditionalOperator?:-Program,Readinacharacter,ifitisanuppercase,outputitslowercaseequivalent.Otherwiseoutputtheread-incharacter.Step1:Readacharacterintovariablech.Step2:Ifchisanuppercase,convertitintoitslowercaseequivalent.Step3:Outputch.,.,ConditionalOperator?:-Program,main()charch;printf(Pleaseinputthecharactor:n);scanf(%c,ch=(ch=A,printf(Thecharactoris:%cn,(ch=A,.,switchStatement,Onegeneralifstatementcontrols2branches.Inordertodealwithmorethan2branches,wecanusethenestingstructureofifstatement.However,themorebranches,themorenestinglevel,thelongerprogramlines,themoredifficultlytoread.Csuppliesamultiwaydecisionstatement:switch.,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,.,switchStatement,Accordingtothegrade,outputinformation.GradeA:outputExcellent!.GradeB:outputGood!.GradeC:outputPass!.GradeD:outputFail!.Others:outputDataerror!,switch(grade)caseA:printf(Excellent!);caseB:printf(Good!);caseC:printf(Pass!);caseD:printf(Fail!);default:printf(Dataerror!);,ifgrade=B,Good!Pass!Fail!Dataerror!,.,switchStatement,Accordingtothegrade,outputinformation.GradeA:outputExcellent!.GradeB:outputGood!.GradeC:outputPass!.GradeD:outputFail!.Others:outputDataerror!,switch(grade)caseA:printf(Excellent!);caseB:printf(Good!);caseC:printf(Pass!);caseD:printf(Fail!);default:printf(Dataerror!);,switch(grade)caseA:printf(Excellent!);break;caseB:printf(Good!);break;caseC:printf(Pass!);break;caseD:printf(Fail!);break;default:printf(Dataerror!);,Good!,ifgrade=B,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,shouldbeintegerorcharactertype,Ifitisnotintegerorcharactertypeexpression,itwillbeconvertedtointegertype.,Forallthat,youshouldnotuserealnumberexpressionaspossible.,switch(2.8)case1:printf(1);break;case2:printf(2);break;case3:printf(3);break;default:printf(0);,2,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,shouldbeintegerorcharactertype,mustbeconstantsorconstantsexpressions,andcantberealnumber,switch(2)case1:printf(1);break;case2:printf(2);break;case3:printf(3);break;default:printf(0);,2,switch(2)case1:printf(1);break;case1+1:printf(2);break;case3:printf(3);break;default:printf(0);,2,inta=1;switch(2)case1:printf(1);break;case1+a:printf(2);break;case3:printf(3);break;default:printf(0);,switch(2)case1:printf(1);break;case2.0:printf(2);break;case3:printf(3);break;default:printf(0);,Error.:Constantexpressionrequiredinfunction.,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,shouldbeintegerorcharactertype,mustbeconstantsorconstantsexpressions,andcantberealnumber,switch(2)case1:printf(1);break;case2:printf(2);break;case1+1:printf(3);break;default:printf(0);,Eachofthesevaluesmustbeuniquewithinaswitchstatement.,Error.:Duplicatecaseinfunction.,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,canbezeroormorestatements,andneedntputbracesaroundthesestatements.,Inifstatement,thestatementafteriforelsemustbeasinglestatementoracompoundstatement.,switch(x+y)case1:printf(1);printf(n);break;case2:printf(2);printf(n);break;default:printf(0);printf(n);,Payattentiontotheusageofbreak.,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,multiplecasemayshareonestatementgroup.,switch(x+y)case1:case2:case3:printf(1);printf(n);break;default:printf(0);,.,switchStatement,Thegeneralform:,switch(exp)casevalue1:statementgroup1;casevalue2:statementgroup2;.casevaluen:statementgroupn;default:statementgroup;,isanoptionalcase,andcanbeplacedanywherebutusuallyplacedattheend.,Ifpresent,itwillbeexecutedwhentheexpressiondoesnotmatchwithanyofthecasevalues.,switch(0)case1:printf(1);break;case2:printf(2);break;case3:printf(3);break;default:printf(0);,0,switch(0)case1:printf(1);break;default:printf(0);case2:printf(2);break;case3:printf(3);break;,02,.,switchStatementProgram1,Accordingtothescore,decidethegrade.score=90:gradeA;score=70,Step1:readinscore(floattype).Step2:Accordingto(int)score/10,decideandoutputthegrade.,.,switchStatementProgram1,main()floatscore;printf(Pleaseinputthescore
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 占道管理面试技巧探讨
- 尽职调查经理岗位培训计划
- (完整版)苏教六年级下册期末数学专题资料真题经典套题
- (完整版)苏教六年级下册期末数学测试题目A卷答案
- 商标撤销案件调解与和解技巧总结
- 客服经理年度总结报告
- 医院招聘临床医生技巧指导
- 教师资格面试常见问题指南
- 教育行业岗位选择要点解析
- 内蒙古沃丰农业发展有限公司招聘55名工作笔试历年参考题库附带答案详解
- 陕西庙会会谱
- 最全地理顺口溜(初高中均适用)
- 旅游报名登记表
- 扬州市不动产登记申请书
- TISA 004-2021 沙棘果油生产标准
- GB/T 3766-2015液压传动系统及其元件的通用规则和安全要求
- GB/T 28733-2012固体生物质燃料全水分测定方法
- GB/T 1226-2017一般压力表
- GB 16668-2010干粉灭火系统及部件通用技术条件
- GA/T 508-2004道路交通信号倒计时显示器
- GA 1517-2018金银珠宝营业场所安全防范要求
评论
0/150
提交评论