programming-in-ANSI-C-Chapter-5-Decision-Making-an

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter5DecisionMakingandBranchingPROGRAMMINGINANSIC210/8/2019QuestionQuestions:Howdowejudgewhetherastudentpassanexaminationaccordingtohisscore?Howdowedecidehisgradeaccordingtohisscore?Inhumannaturelanguage:If…,then…InC:Decision-makingstatement(branchstatement)310/8/2019Chapter5Inthischapter,wewilllearn:Decision-makingstatement:ifConditionaloperator:?:Multiwaydecision-makingstatement:switchAssistedcontrolstatement:break410/8/20193FormsofifStatementForm1–themostsimpleformif(testexpression)statement;testexpstatementtrue(not0)false(0)if(score=60)printf(Hepassedthisexamination!);510/8/20193FormsofifStatementForm2–themostgeneralformif(testexpression)statement1;elsestatement2;if(score=60)printf(Hepassedthisexamination!);elseprintf(Hefailedinthisexamination!);testexpstatement1true(not0)false(0)statement2610/8/20193FormsofifStatementForm3–thenestedformif(exp1)s1;elseif(exp2)s2;else……elseif(expn)sn;elses;exp1s1truefalseexp2falseexpnfalses2……snstruetrueif(score=90)grade='A';elseif(score=80)grade='B';elseif(score=70)grade='C';elseif(score=60)grade='D';elsegrade='E';710/8/2019ifStatementThevalueofthetestexpressionmaybeanytype.Ifitequalszero,itisfalse,otherwisetrue.if(a==b&&x==y)printf(a=b,x=y);if(3)printf(OK);OK810/8/2019if(a=2)printf(%d,a);elseprintf(“Wrong);if(a=0)printf(%d,a);elseprintf(“Wrong);ifStatementThevalueofthetestexpressionmaybeanytype.Ifitequalszero,itisfalse,otherwisetrue.2Wrong910/8/2019ifStatementThestatementfollowingiforelsemaybeasinglestatementoracompoundstatement.main(){intx,y;scanf(%d,%d,&x,&y);if(xy)x=y;y=x;elsex++;y++;printf(%d,%d\n,x,y);}CompileError!Error…6:Misplacedelseinfunctionmain1010/8/2019ifStatement–Program1Input2realnumbers,andoutputtheminascendingorder.Step1:Read2realnumbersintovariablexandy.Step2:Ifxisgreaterthany,exchangethem.Step3:Outputxandy.xyexchangexandytruefalseoutputxandy1110/8/2019ifStatement–Program1main(){floatx,y,temp;printf(Pleaseinput2numbers:\n);scanf(%f%f,&x,&y);if(xy){temp=x;x=y;y=temp;}printf(The2numbersare:%5.2f,%5.2f\n,x,y);}Pleaseinput2numbers:13.22.1The2numbersare:2.10,13.201210/8/2019ifStatement–Program1Input2realnumbers,andoutputtheminascendingorder.Step1:Read2realnumbersintovariablexandy.Step2:Ifxislessthany,outputxandy,orelseoutputyandx.xyoutputxandytruefalseoutputyandx1310/8/2019ifStatement–Program1main(){floatx,y;printf(Pleaseinput2numbers:\n);scanf(%f%f,&x,&y);printf(The2numbersare:);if(xy)printf(%5.2f,%5.2f\n,x,y);elseprintf(%5.2f,%5.2f\n,y,x);}1410/8/2019ifStatement–Program1ProgrammingExercises:Input3realnumbers,andoutputtheminascendingorder.(Homework!)1510/8/2019ifStatement–Program2Readinoneyear,andjudgewhetheritisaleapyearornot.Step1:Readintheyear.Step2:Ifthetestexpression(year%4==0&&year%100!=0)||(year%400==0)istrue,thenoutputitisaleapyear,orelseoutputitisn’taleapyear.1610/8/2019ifStatement–Program2main(){intyear;printf(Pleaseinputtheyear:\n);scanf(%d,&year);if((year%4==0&&year%100!=0)||(year%400==0))printf(%disaleapyear!\n,year);elseprintf(%disnotaleapyear!\n,year);}Pleaseinputtheyear:19001900isnotaleapyear!1710/8/2019ifStatement–Program2Step1:Settheflagvariableisleapas0.Step2:Readintheyear.Step3:Ifthetestexpression(year%4==0&&year%100!=0)||(year%400==0)istrue,settheflagvariableisleapas1.Step4:Accordingtothevalueofisleap,outputtheyearisaleapyearornot.1810/8/2019ifStatement–Program2main(){intyear,isLeap=0;printf(Pleaseinputtheyear:\n);scanf(%d,&year);if((year%4==0&&year%100!=0)||(year%400==0))isLeap=1;if(isLeap)printf(%disaleapyear!\n,year);elseprintf(%disnotaleapyear!\n,year);}Pleaseinputtheyear:20002000isaleapyear!1910/8/2019ifStatement–Program3Readinonecharacter,andjudgewhichkindofcharacteritis:afigure,oraletter,orother.Step1:Readthecharacterintovariablech.Step2:If(ch='0'&&ch='9'),itisafigure.If(ch='a'&&ch='z')||(ch='A'&&ch='Z'),itisaletter.Otherwiseitisanothercharacter.2010/8/2019ifStatement–Program3#includestdio.hmain(){charch;printf(Pleaseinputthecharacter:\n);ch=getchar();if(ch='0'&&ch='9')printf(%cisafigure!,ch);elseif((ch='a'&&ch='z')||(ch='A'&&ch='Z'))printf(%cisaletter!,ch);elseprintf(%cisanothercharacter!,ch);}Pleaseinputthecharacter:MMisaletter!2110/8/2019Nestingofif…else…StatementWhenaseriesofdecisionareinvolved,wemayusemorethanoneif…else…statementinnestedform.if(exp1)if(exp2)statement1;elsestatement2;elseif(exp3)statement3;elsestatement4;2210/8/2019Nestingofif…else…Statement“else”matchingprinciple:Anelseisalwayslinkedtotheclosestnon-terminatedif.if(exp1)if(exp2)statement1;elsestatement2;if(exp1){if(exp2)statement1;}elsestatement2;2310/8/2019ifStatement–Program4Accordingtothescore,decidethegrade.score=80:gradeA;score=60&&score80:gradeB;score60:gradeC;2410/8/2019ifStatement–Program4main(){floatscore;printf(Pleaseinputthescore:\n);scanf(%f,&score);if(score=80)printf(Thegradeis:A!\n);elseif(score=60)printf(Thegradeis:B!\n);elseprintf(Thegradeis:C!\n);}Pleaseinputthescore:75.5ThegradeisB!2510/8/2019ifStatement–Program4main(){floatscore;chargrade;printf(Pleaseinputthes

1 / 52
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功