您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > C语言学习第二章(英文版).ppt
Thenumberofthegroup:77162023Chapter2AlgorithmandFlowchart2.1Whatisalgorithm2.2Featuresofalgorithms2.3Pseudocode2.4Flowchart2.5ControlStructures2.6SelectionStructure2.7RepetitionStructureOutlineKeypointsWhatisalgorithmFeaturesofalgorithmsFlowchart2.1WhatisalgorithmBeforewritingaprogram:HaveathoroughunderstandingoftheproblemCarefullyplananapproachforsolvingitWhilewritingaprogram:Knowwhat”buildingblocks”areavailableUsegoodprogrammingprinciples2.1WhatisalgorithmAstep-by-stepproblem-solvingprocedure,especiallyanestablished,recursivecomputationalprocedureforsolvingaprobleminafinitenumberofsteps.2.2FeaturesofalgorithmDeterminismAnalgorithmisdeterministic,ifatanystageofthealgorithmexecution,thenextactionstepisclearlydefined.FinitenessThefinitenessrestrictsthedefinitionofthealgorithmtoafinitelong-termoperation.EffectivenessTheeffectofeachstatementofanalgorithmmustbeclearlydefined.2.3PseudocodePseudocodeArtificial,informallanguagethathelpsusdevelopalgorithmsSimilartoeverydayEnglishNotactuallyexecutedoncomputersHelpsus“thinkout”aprogrambeforewritingitEasytoconvertintoacorrespondingCprogramConsistsonlyofexecutablestatements2.3PseudocodeExampleofpseudocodeIfstudent’sgradeisgreaterthanorequalto60Print“Passed”Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”elsePrint“Failed”2.4FlowchartFlowchartGraphicalrepresentationofanalgorithmDrawnusingcertainspecial-purposesymbolsconnectedbyarrowscalledflowlinesRectanglesymbol(actionsymbol):IndicatesanytypeofactionOvalsymbol:IndicatesthebeginningorendofaprogramorasectionofcodeSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructuresSequentialexecutionStatementsexecutedoneaftertheotherintheorderwrittenTransferofcontrolWhenthenextstatementexecutedisnotthenextoneinsequenceOveruseofgotostatementsledtomanyproblemsBohmandJacopiniAllprogramswrittenintermsof3controlstructuresSequencestructures:BuiltintoC.ProgramsexecutedsequentiallybydefaultSelectionstructures:Chasthreetypes:if,if/else,andswitch(bookchapter4)Repetitionstructures:Chasthreetypes:while,do/whileandforSingle-entry/single-exitcontrolstructuresConnectexitpointofonecontrolstructuretoentrypointofthenext(control-structurestacking)Makesprogramseasytobuild2.5ControlStructures2.6SelectionStructure(if)Selectionstructure:UsedtochooseamongalternativecoursesofactionPseudocode:Ifstudent’sgradeisgreaterthanorequalto60Print“Passed”IfconditiontruePrintstatementexecutedandprogramgoesontonextstatementIffalse,printstatementisignoredandtheprogramgoesontothenextstatementIndentingmakesprogramseasiertoreadCignoreswhitespacecharacters2.6SelectionStructure(if)PseudocodestatementinC:if(grade=60)printf(Passed\n);CcodecorrespondscloselytothepseudocodeDiamondsymbol(decisionsymbol)IndicatesdecisionistobemadeContainsanexpressionthatcanbetrueorfalseTestthecondition,followappropriatepath2.6SelectionStructure(if)ifstructureisasingle-entry/single-exitstructureyesnograde=60print“Passed”Adecisioncanbemadeonanyexpression.zero-nononzero-yesExample:3-4isyes2.6SelectionStructure(if)Page32Ifyoutypeanumberlessthan10,yougetamessage“Whatanobedientservantyouare!”onthescreen.Otherwise,theprogramdoesn’tdoanything.STARTPRINTenteranumlessthan10INPUTnumisnum10PRINTWhatanobedientservantyouare!STOPyesno2.6SelectionStructure(if/else)ifOnlyperformsanactioniftheconditionistrueif/elseSpecifiesanactiontobeperformedbothwhentheconditionistrueandwhenitisfalsePsuedocode: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/elseselectionstructureNestedif/elsestructuresTestformultiplecasesbyplacingif/elseselectionstructuresinsideif/elseselectionstructuresOnceconditionismet,restofstatementsskippedDeepindentationusuallynotusedinpracticeyesnoprint“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:SetofstatementswithinapairofbracesExample:if(grade=60)printf(Passed.\n);else{printf(Failed.\n);printf(Youmusttakethiscourseagain.\n);}Withoutthebraces,thestatementprintf(Youmusttakethiscourseagain.\n);wouldbeexecutedautomatically2.6SelectionStructure(if/else)Block:CompoundstatementswithdeclarationsSyntaxerrorsCaughtbycompilerLogicerrors:HavetheireffectatexecutiontimeNon-fatal:programruns,b
本文标题:C语言学习第二章(英文版).ppt
链接地址:https://www.777doc.com/doc-1198964 .html