今天我们继续学习C语言程序设计第5章循环控制结构的内容。今天的主要内容有两部分,分别是条件控制的循环,流程的转移控制。TodaywecontinueourstudyofCProgrammingChapter5LoopControlStructures。Thetwomainpartsoftodaystopicareloopsforconditionalcontrol,andtransfercontrolofprocesses。 No。1条件控制的循环 循环次数事先未知的循环通常是由一个条件控制的,称为条件控制的循环。 Loopswherethenumberofcyclesisunknowninadvanceareusuallycontrolledbyaconditionandarecalledconditioncontrolledloops。 No。2流程的转移控制 goto语句,break语句,continue语句和return语句是c语言中用于控制流程转移的跳转语句。 1。goto语句为无条件转向语句,它既可以向下跳转,也可往回跳转。 它的作用是在不需要任何条件的情况下直接使程序跳转到该语句标号所标识的语句去执行。 Thegotostatement,breakstatement,continuestatementandreturnstatementarejumpstatementsusedinclanguagetocontroltheflowtransfer。 Thegotostatementisanunconditionalsteeringstatementthatcaneitherjumpdownorbackup。 Itsfunctionistomaketheprogramjumpdirectlytothestatementidentifiedbythestatementmarkertoexecutewithoutanyconditions。 2。break语句除用于退出switch结构外,还可用于while,dowhile和for构成的循环语句的循环体中。当执行循环体遇到break语句时,循环将立即终止,从循环语句后的第一条语句开始继续执行。如果使用的是嵌套循环(即一个循环内嵌套另一个循环),break语句会停止执行最内层的循环,然后开始执行该块之后的下一行代码。 2。Thebreakstatementcanbeusedintheloopbodyofwhile,dowhileandforloops,inadditiontoexitingswitchstructures。Whenabreakstatementisencounteredinthebodyofaloop,theloopisimmediatelyterminatedandexecutioncontinuesfromthefirststatementaftertheloopstatement。Ifanestedloopisused(i。e。oneloopnestedwithinanother),thebreakstatementwillstoptheexecutionoftheinnermostloopandstarttheexecutionofthenextlineofcodeafterthatblock。 3。continue语句与break语句都可用于对循环进行内部控制,但二者对流程的控制效果是不同的。当在循环体中遇到continue语句时,程序将跳过continue语句后面尚未执行的语句,开始下一次循环,即只结束本循环的执行,并不终止整个循环的执行。 3。Thecontinuestatementandthebreakstatementcanbothbeusedtocontroltheloopinternally,buttheeffectofthetwoontheflowisdifferent。Whenacontinuestatementisencounteredintheloopbody,theprogramwillskipthestatementsthathavenotyetbeenexecutedafterthecontinuestatementandstartthenextloop,i。e。itonlyendstheexecutionoftheloopanddoesnotterminatetheexecutionofthewholeloop。 4。break与continue的对比:break用来结束所有循环,循环语句不再有执行的机会;continue用来结束本次循环,直接跳到下一次循环,如果循环条件成立,还会继续循环。 4。breakvs。continue:breakisusedtoendallloopsandtheloopstatementnolongerhasachancetoexecute;continueisusedtoendtheloopandjumpdirectlytothenextloop,whichwillcontinueiftheloopconditionholds。 参考资料:文字:百度;图片:微博;翻译:百度翻译 本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。