国外C语言程序设计英文课件-lectur.ppt

上传人:自*** 文档编号:127275943 上传时间:2020-03-31 格式:PPT 页数:34 大小:210KB
返回 下载 相关 举报
国外C语言程序设计英文课件-lectur.ppt_第1页
第1页 / 共34页
国外C语言程序设计英文课件-lectur.ppt_第2页
第2页 / 共34页
国外C语言程序设计英文课件-lectur.ppt_第3页
第3页 / 共34页
国外C语言程序设计英文课件-lectur.ppt_第4页
第4页 / 共34页
国外C语言程序设计英文课件-lectur.ppt_第5页
第5页 / 共34页
点击查看更多>>
资源描述

《国外C语言程序设计英文课件-lectur.ppt》由会员分享,可在线阅读,更多相关《国外C语言程序设计英文课件-lectur.ppt(34页珍藏版)》请在金锄头文库上搜索。

1、Computerprogramming Lecture3 Lecture3 Outline ProgramLooping Kochan chap 5 TheforStatementRelationalOperatorsNestedforLoopsIncrementOperatorProgramInputforLoopVariantsThewhileStatementThedoStatementThebreakStatementThecontinueStatement Executingaprogram Program listofstatementsEntrypoint thepointwhe

2、retheexecutionstartsControlflow theorderinwhichtheindividualstatementsareexecuted Statement1Statement2Statement3Statement4Statement5Statement6Statement7Statement8 StructureofaCprogram includeintmain void intvalue1 value2 sum value1 50 value2 25 sum value1 value2 printf Thesumof iand iis i n value1 v

3、alue2 sum return0 EntrypointofaCprogram Sequentialflowofcontrol Controllingtheprogramflow Formsofcontrollingtheprogramflow ExecutingasequenceofstatementsRepeatingasequenceofstatements untilsomeconditionismet looping Usingatesttodecidebetweenalternativesequences branching Statement1Statement2Statemen

4、t3Statement4Statement5Statement6Statement7Statement8 ProgramLooping Looping doingonethingoverandoverProgramloop asetofstatementsthatisexecutedrepetitivelyforanumberoftimesSimpleexample displayingamessage100times printf hello n printf hello n printf hello n printf hello n printf hello n Repeat100time

5、sprintf hello n Programlooping enablesyoutodevelopconciseprogramscontainingrepetitiveprocessesthatcouldotherwiserequiremanylinesofcode Theneedforprogramlooping includeintmain void inttriangularNumber triangularNumber 1 2 3 4 5 6 7 8 printf Theeighthtriangularnumberis i n triangularNumber return0 Wha

6、tifwehavetocomputethe200 th 1000 th etc triangularnumber Exampleproblem computingtriangularnumbers Then thtriangularnumberisthesumoftheintegersfrom1throughn InC 3differentstatementsforlooping for while do Example 200thtriangularnumber n 1 triangularNumber triangularNumber n n 200 n n 1 yes no triang

7、ularNumber 0 PrinttriangularNumber Statementbeforeloop init expression loop condition statement loop expression Statementafterloop Example for Programtocalculatethe200thtriangularnumberIntroductionoftheforstatement includeintmain void intn triangularNumber triangularNumber 0 for n 1 n 200 n n 1 tria

8、ngularNumber triangularNumber n printf The200thtriangularnumberis i n triangularNumber return0 Theforstatement for init expression loop condition loop expression programstatement init expression Programstatement loop condition Loopexpression yes no 1 2 3 4 5 Theforstatement for n 1 n 200 n n 1 trian

9、gularNumber triangularNumber n 1 2 3 4 5 yes no Howforworks Theexecutionofaforstatementproceedsasfollows 1 Theinitialexpressionisevaluatedfirst Thisexpressionusuallysetsavariablethatwillbeusedinsidetheloop generallyreferredtoasanindexvariable tosomeinitialvalue 2 Theloopingconditionisevaluated Ifthe

10、conditionisnotsatisfied theexpressionisfalse hasvalue0 theloopisimmediatelyterminated Executioncontinueswiththeprogramstatementthatimmediatelyfollowstheloop 3 Theprogramstatementthatconstitutesthebodyoftheloopisexecuted 4 Theloopingexpressionisevaluated Thisexpressionisgenerallyusedtochangethevalueo

11、ftheindexvariable5 Returntostep2 Infiniteloops It sthetaskoftheprogrammertodesigncorrectlythealgorithmssothatloopsendatsomemoment Programtocount1 2 3 4 5 includeintmain void inti n 5 sum 0 for i 1 i n n n 1 sum sum i printf i i i n i sum n return0 Whatiswronghere Doestheloopend Relationaloperators T

12、herelationaloperatorshavelowerprecedencethanallarithmeticoperators a b cisevaluatedasa b c ATTENTION Donotconfuse the isequalto operator andthe assignment operator ATTENTIONwhencomparingfloating pointvalues Onlycomparisonsmakesense Example forwithabodyof2 Programtogenerateatableoftriangularnumbers i

13、ncludeintmain void intn triangularNumber printf TABLEOFTRIANGULARNUMBERS n n printf nSumfrom1ton n printf n triangularNumber 0 for n 1 n 10 n triangularNumber n printf i i n n triangularNumber return0 Thebodyoftheloopconsistsinablockof2statements Incrementoperator Becauseadditionby1isaverycommonoper

14、ationinprograms aspecialoperatorwascreatedinCforthis Incrementoperator theexpression nisequivalenttotheexpressionn n 1 Decrementoperator theexpression nisequivalenttotheexpressionn n 1Incrementanddecrementoperatorscanbeplacedinfront prefix orafter postfix theiroperand Thedifferencebetweenprefixandpo

15、stfix Example ifn 4 a n leadstoa 4 n 5a nleadstoa 5 n 5 Programinput includeintmain void intn number triangularNumber printf Whattriangularnumberdoyouwant scanf i Scanf similartoprintf firstargumentcontainsformatcharacters nextargumentstellwheretostorethevaluesenteredatthekeyboardMoredetails inalate

16、rchapter Readsintegerfromkeyboard It spolitetodisplayamessagebefore Nestedloops includeintmain void intn number triangularNumber counter for counter 1 counter 5 counter printf Whattriangularnumberdoyouwant scanf i Rememberindentations forloopvariants Multipleexpressions commabetween for i 0 j 10 i j i j Omittingfields semicolonhavetobestill i 0 for i 10 i Declaringvariablesfor inti 0 i 10 i Thewhilestatement while expression programstatement while number0 printf Giveanewnumber scanf i Thewhilest

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号