C语言学习第二章(英文版).ppt

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

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

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

资源描述

Thenumberofthegroup:77162023Chapter2AlgorithmandFlowchart2.1Whatisalgorithm2.2Featuresofalgorithms2.3Pseudocode2.4Flowchart2.5ControlStructures2.6SelectionStructure2.7RepetitionStructureOutlineKeypointsWhatisalgorithmFeaturesofalgorithmsFlowchart2.1WhatisalgorithmBeforewritingaprogram:HaveathoroughunderstandingoftheproblemCarefullyplananapproachforsolvingitWhilewritingaprogram:Knowwhat”buildingblocks”areavailableUsegoodprogrammingprinciples2.1WhatisalgorithmAstep-by-stepproblem-solvingprocedure,especiallyanestablished,recursivecomputationalprocedureforsolvingaprobleminafinitenumberofsteps.2.2FeaturesofalgorithmDeterminismAnalgorithmisdeterministic,ifatanystageofthealgorithmexecution,thenextactionstepisclearlydefined.FinitenessThefinitenessrestrictsthedefinitionofthealgorithmtoafinitelong-termoperation.EffectivenessTheeffectofeachstatementofanalgorithmmustbeclearlydefined.2.3PseudocodePseudocodeArtificial,informallanguagethathelpsusdevelopalgorithmsSimilartoeverydayEnglishNotactuallyexecutedoncomputersHelpsus“thinkout”aprogrambeforewritingitEasytoconvertintoacorrespondingCprogramConsistsonlyofexecutablestatements2.3PseudocodeExampleofpseudocodeIfstudent’sgradeisgreaterthanorequalto60Print“Passed”Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”elsePrint“Failed”2.4FlowchartFlowchartGraphicalrepresentationofanalgorithmDrawnusingcertainspecial-purposesymbolsconnectedbyarrowscalledflowlinesRectanglesymbol(actionsymbol):IndicatesanytypeofactionOvalsymbol:IndicatesthebeginningorendofaprogramorasectionofcodeSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructuresSequentialexecutionStatementsexecutedoneaftertheotherintheorderwrittenTransferofcontrolWhenthenextstatementexecutedisnotthenextoneinsequenceOveruseofgotostatementsledtomanyproblemsBohmandJacopiniAllprogramswrittenintermsof3controlstructuresSequencestructures:BuiltintoC.ProgramsexecutedsequentiallybydefaultSelectionstructures:Chasthreetypes:if,if/else,andswitch(bookchapter4)Repetitionstructures:Chasthreetypes:while,do/whileandforSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructures2.6SelectionStructure(if)Selectionstructure:UsedtochooseamongalternativecoursesofactionPseudocode:Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”IfconditiontruePrintstatementexecutedandprogramgoesontonextstatementIffalse,printstatementisignoredandtheprogramgoesontothenextstatementIndentingmakesprogramseasiertoreadCignoreswhitespacecharacters2.6SelectionStructure(if)PseudocodestatementinC:if(grade=60)printf(Passed\n);CcodecorrespondscloselytothepseudocodeDiamondsymbol(decisionsymbol)IndicatesdecisionistobemadeContainsanexpressionthatcanbetrueorfalseTestthecondition,followappropriatepath2.6SelectionStructure(if)ifstructureisasingle-entry/single-exitstructureyesnograde=60print“Passed”Adecisioncanbemadeonanyexpression.zero-nononzero-yesExample:3-4isyes2.6SelectionStructure(if)Page32Ifyoutypeanumberlessthan10,yougetamessage“Whatanobedientservantyouare!”onthescreen.Otherwise,theprogramdoesn’tdoanything.STARTPRINTenteranumlessthan10INPUTnumisnum10PRINTWhatanobedientservantyouare!STOPyesno2.6SelectionStructure(if/else)ifOnlyperformsanactioniftheconditionistrueif/elseSpecifiesanactiontobeperformedbothwhentheconditionistrueandwhenitisfalsePsuedocode:Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”elsePrint“Failed”Notespacing/indentationconventions2.6SelectionStructure(if/else)Ccode:if(grade=60)printf(Passed\n);elseprintf(Failed\n);Conditionaloperator(?:)Takesthreearguments(condition,valueiftrue,valueiffalse)Ourpseudocodecouldbewritten:printf(%s\n,grade=60?Passed:Failed);Oritcouldhavebeenwritten:grade=60?printf(“Passed\n”):printf(“Failed\n”);2.6SelectionStructure(if/else)Flowchartoftheif/elseselectionstructureNestedif/elsestructuresTestformultiplecasesbyplacingif/elseselectionstructuresinsideif/elseselectionstructuresOnceconditionismet,restofstatementsskippedDeepindentationusuallynotusedinpracticeyesnoprint“Failed”print“Passed”grade=602.6SelectionStructure(if/else)Pseudocodeforanestedif/elsestructureIfstudent’sgradeisgreaterthanorequalto90Print“A”elseIfstudent’sgradeisgreaterthanorequalto80Print“B”elseIfstudent’sgradeisgreaterthanorequalto70Print“C”elseIfstudent’sgradeisgreaterthanorequalto60Print“D”elsePrint“F”2.6SelectionStructure(if/else)Compoundstatement:SetofstatementswithinapairofbracesExample:if(grade=60)printf(Passed.\n);else{printf(Failed.\n);printf(Youmusttakethiscourseagain.\n);}Withoutthebraces,thestatementprintf(Youmusttakethiscourseagain.\n);wouldbeexecutedautomatically2.6SelectionStructure(if/else)Block:CompoundstatementswithdeclarationsSyntaxerrorsCaughtbycompilerLogicerrors:HavetheireffectatexecutiontimeNon-fatal:programruns,b

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

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

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

×
保存成功