欢迎来到天天文库
浏览记录
ID:48029150
大小:169.50 KB
页数:34页
时间:2020-01-11
《国外C语言程序设计英文课件-lecture3.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、ComputerprogrammingLecture3Lecture3:OutlineProgramLooping[Kochan–chap.5]TheforStatementRelationalOperatorsNestedforLoopsIncrementOperatorProgramInputforLoopVariantsThewhileStatementThedoStatementThebreakStatementThecontinueStatementExecutingaprogramProgram=listofstatementsEntrypoint:thepoin
2、twheretheexecutionstartsControlflow:theorderinwhichtheindividualstatementsareexecutedStatement1Statement2Statement3Statement4Statement5Statement6Statement7Statement8StructureofaCprogram#includeintmain(void){intvalue1,value2,sum;value1=50;value2=25;sum=value1+value2;printf("Thesumof
3、%iand%iis%i",value1,value2,sum);return0;}EntrypointofaCprogramSequentialflowofcontrolControllingtheprogramflowFormsofcontrollingtheprogramflow:ExecutingasequenceofstatementsRepeatingasequenceofstatements(untilsomeconditionismet)(looping)Usingatesttodecidebetweenalternativesequences(branch
4、ing)Statement1Statement2Statement3Statement4Statement5Statement6Statement7Statement8ProgramLoopingLooping:doingonethingoverandoverProgramloop:asetofstatementsthatisexecutedrepetitivelyforanumberoftimesSimpleexample:displayingamessage100times:printf(“hello!”);printf(“hello!”);printf(“hel
5、lo!”);…printf(“hello!”);printf(“hello!”);Repeat100timesprintf(“hello!”);Programlooping:enablesyoutodevelopconciseprogramscontainingrepetitiveprocessesthatcouldotherwiserequiremanylinesofcode!Theneedforprogramlooping#includeintmain(void){inttriangularNumber;triangularNumber=
6、1+2+3+4+5+6+7+8;printf("Theeighthtriangularnumberis%i",triangularNumber);return0;}Whatifwehavetocomputethe200-th(1000-th,etc)triangularnumber?Exampleproblem:computingtriangularnumbers.(Then-thtriangularnumberisthesumoftheintegersfrom1throughn)InC:3differentstatementsforlooping:for,while,d
7、oExample–200thtriangularnumbern=1triangularNumber=triangularNumber+nn<=200n=n+1yesnotriangularNumber=0PrinttriangularNumberStatementbeforeloopinit_expressionloop_conditionstatementloop_expressionStatementafterloopExample-for/*Programtocalculatethe200thtr
此文档下载收益归作者所有