资源描述:
《c语言程序设计课件ppt(英文)c program language 之4 control flows》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、ControlflowsChapterFourContentsBasiccontrolflowsinC1Ifstatement2Switchstatement3Loopstructure4Breakandcontinuestatements5gotoandlabels6BasiccontrolflowsinCThecontrol-flowofalanguagespecifytheorderinwhichcomputationsareperformed.SequenceStatementsareexecutedintheord
2、erofwrittingConditionTestthecondition,whentheresultistrue,executeA,otherwise,executeBLoopRepeatsomestatements.Whenconditionistrue,repeatA,stopwhilefalseRepeatA,stopwhentheconditionistrue.ABBasiccontrolflowsinCexpABYNForeachcontrolflow,thereisonlyonedataentrance,one
3、dataexit.YNstatementexpIfstatementHowcanwedividethescoresintopassandfail?Howcanwechangethescoresintofivegrades?Innatuallanguage:if……thenInClanguage:ifstatementTheif–elsestatementisusedtoexpressdecisions.Formallythesyntaxis:if(expression)statement1elsestatement2Wher
4、etheelsepartisoptional.Theexpressionisevaluated;ifitistrue,statement1isexecuted.Ifitisfalseandifthereisanelsepart,statement2isexcutedinstead.casesIfstatement(3)if(exp1)statement1elseif(exp2)statement2elseif(exp3)statement3……elsestatementn1、Threekindsofifstatement(1
5、)if(expression)statementexpstatementYN(2)if(expression)statement1elsestatement2expYNstatement2statement1exp1exp3statement3statementnexp2statement1Ystatement2YYNNNMulti-waydecisionIfstatementTheexpressionsareevaluatedinorder;ifanexpressionistrue,thestatementassociat
6、edwithitisexected,andthisterminatesthewholechain.IfstatementMulti-waydecision12Thissequenceofifstatementsisthemostgeneralwayofwritingamulti-waydecision.Thecodeforeachstatementiseithersinglestatement,oragroupoftheminbraces.3Thelastelseparthandlesthe“noneoftheabove“o
7、radefaultcasewherenoneoftheotherconditionsissatisfied.main(){inta;scanf(“%d”,&a);if(a<10)printf(“%d<10”,a);elseif(a<100)printf(“10<%d<100”,a);elseif(a<1000)printf(“<100<%d<1000”,a);elseprintf(“%d>1000”,a);}1010&&A<100Ifstatement2nesting-ifstatementi
8、f(exp)if(exp1)statement11elsestatement12elseif(exp2)statement21elsestatement22Ifstatementexpstatement11exp2exp1YYYNNNstatement12statement21statem