Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C1Chapter4-FlowofControlOh!Thepowerofselectionandrepetition……Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C24.1Introduction4.2LogicalOperators4.3ConfusingEquality(==)andAssignment(=)Operators4.4TheIfSelectionStructure4.5TheIf/ElseSelectionStructure4.6TheSwitchMultiple-SelectionStructure4.7TheEssentialsofRepetition4.8TheWhileRepetitionStructure4.9TheForRepetitionStructure4.10TheDo…WhileRepetitionStructure4.11ThebreakandcontinueStatements4.12StructuredProgrammingSummaryOutlineChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C34.1IntroductionThischapterintroducesSelectionstructureifif/elseswitchmultipleAdditionalrepetitioncontrolstructuresforwhile,do/whileBreakstatementUsedforexitingimmediatelyandrapidlyfromcertaincontrolstructuresContinuestatementUsedforskippingtheremainderofthebodyofarepetitionstructureandproceedingwiththenextiterationoftheloopChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C4KeypointsTheIfSelectionStructureTheIf/ElseSelectionStructureTheWhileRepetitionStructureTheForRepetitionStructureChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C54.2LogicalOperators&&(logicalAND)Returnstrueifbothconditionsaretrue||(logicalOR)Returnstrueifeitherofitsconditionsaretrue!(logicalNOT,logicalnegation)Reversesthetruth/falsityofitsconditionUnaryoperator,hasoneoperandUsefulasconditionsinselectionsandloopsExpressionResulttrue&&falsefalsetrue||falsetrue!falsetrueChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C64.2LogicalOperatorsTruthTablesX&&YYXFalseFalseFalseFalseTrueFalseFalseFalseTrueTrueTrueTrueX||YYXFalseFalseFalseTrueTrueFalseTrueFalseTrueTrueTrueTrue!XXTrueFalseFalseTrueNOTORANDChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C74.2LogicalOperatorsOperatorprecedenceandassociativitySummary(、)、++(postfix)、--(postfix)lefttoright+、-、!、++(prefix)、--(prefix)righttoleft*、/、%lefttoright+、-lefttoright、=、、=lefttoright==、!=lefttoright&&lefttoright||lefttoright?:righttoleft=、+=、-=、*=、/=、%=righttoleft,lefttorightChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C84.2LogicalOperatorsExamplecharc=‘w’inti=1,j=2,k=-7doublex=7e+33,y=0.001Expression‘a’+1c-i-5*j=k+13j5x-3.333=x+yxx+yChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C94.2LogicalOperatorsExamplecharc=‘B’inti=3,j=3,k=3doublex=0.0,y=2.3Expressioni&&j&&kx||i&&j-3ij&&xy‘A’=c&&c=‘Z’c-1==‘A’||c+1==‘Z’Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C104.2LogicalOperatorsLeapyearproblem:Aleapyearisayearwithoneextradayinserted--February29th--sothatithas366daysinsteadoftheusual365.Thisistokeepthecalendarinsynchronizationwiththeactualorbitoftheeartharoundthesun.Everyyeardivisibleby4isaleapyear.(Forexample,1996wasaleapyear.)Exceptthatyearsdivisibleby100yearsarenotleapyears.(Forexample,1900wasnotaleapyear.)Exceptthatyearsdivisibleby400yearsareleapyears.(So2000willbealeapyearafterall.)Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C114.3ConfusingEquality(==)andAssignment(=)OperatorsDangerouserrorDoesnotordinarilycausesyntaxerrorsAnyexpressionthatproducesavaluecanbeusedincontrolstructuresNonzerovaluesaretrue,zerovaluesarefalseExampleusing==:if(payCode==4)printf(Yougetabonus!\n);Checkspaycode,ifitis4thenabonusisawardedChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C124.3ConfusingEquality(==)andAssignment(=)OperatorsExample,replacing==with=if(payCode=4)printf(Yougetabonus!\n);Thissetspaycodeto44isnonzero,soexpressionistrue,andbonusawardednomatterwhatthepaycodewasLogicerror,notasyntaxerrorAverycommonmistake,butlegalinC,thecompilerdoesn’twarnusaboutit!Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C13Exampleintmain(){inti=2;printf(i=%d\n,i);printf((i==4)=%d\n,i==4);printf(i=%d\n,i);printf((i=4)=%d\n,i=4);printf(i=%d\n,i);}4.3ConfusingEquality(==)andAssignment(=)OperatorsChapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C14Exampleintmain(){inti=2;printf(i=%d\n,i);printf((i==4)=%d\n,i==4);printf(i=%d\n,i);printf((i=4)=%d\n,i=4);printf(i=%d\n,i);}4.3ConfusingEquality(==)andAssignment(=)Operatorsi=2Chapter4FlowofControlFacultyofComputing,FacultyofSoftware,NanchangHangKongUniversityfc.nchu.jx.cn/C15Exampleintmain(){inti=2;printf(i=%d\n,i);printf((i==4)=%d\n,i==4);printf(i=%d\n,i);printf((i=4)=%d\n,i=4);printf(i=%d\n,i);}4.3ConfusingEquality(==)andAssignme