欢迎来到天天文库
浏览记录
ID:49262071
大小:356.50 KB
页数:31页
时间:2020-02-02
《L3_Conditionals.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、3.IntroductiontoConditionalsBooleanexpressionsTheIf-ElseConstructAnd,or,notInsightThroughComputingWhatWeCannotDoSoFarIfthevalueofthearithmeticexpressionDice1+Dice2isseven,thenincreasethevalueofthevariableGamesWonbyone.Wecannotmakeacomputationcontingentuponoth
2、erthings.InsightThroughComputingTheIf-ElseConstructSolvesthisProblemWewillintroducethislanguagefeaturebysolvingproblemsaboutthebehaviorofagivenquadraticonagivenintervalL<=x<=R.InsightThroughComputingAssumeVariablesb,c,L,RareInitializedE.g.,b=input(‘Enterb’:)
3、c=input(‘Enterc’:)L=input(‘EnterL’:)R=input(‘EnterR’:)InsightThroughComputingLRTheSituationInsightThroughComputingWriteafragmentthatprints“yes”ifq(x)increasesacrosstheintervaland“no”ifitdoesnot.Problem1InsightThroughComputingLRNo!InsightThroughComputingLRYes
4、!Requirement:xc<=LInsightThroughComputingSolutionFragmentxc=-b/2;ifxc<=Ldisp(‘Yes’)elsedisp(‘No’)endInsightThroughComputingWriteafragmentthatprintsthemaximumvaluethatq(x)attainsontheinterval.Problem2InsightThroughComputingLRMaximumatLInsightThroughComputingL
5、RMaximumatRDependsonwhetherxcistotherightorleftoftheintervalmidpoint.InsightThroughComputingSolutionFragmentxc=-b/2;Mid=(L+R)/2;ifxc<=MidmaxVal=R^2+b*R+celsemaxVal=L^2+b*L+cendInsightThroughComputingWriteafragmentthatprints“yes”ifxcisintheintervaland“no”ifx
6、cisnotintheinterval.Problem3InsightThroughComputingLRNo!Becausexc7、=xc<=RInsightThroughComputingSayingtheOppositexcisintheinterval[L,R]ifL<=xcandxc<=Rxcisnotintheinterval[L,R]ifxc8、9、(R10、=-b/2;if(L<=xc)&&(xc<=R)disp(‘Yes’)elsedisp(‘No’)endInsightThroughComputingTheif-elseConstructifendelsebooleanexpressionCommandstoexecuteiftheexpressionifTRUECommandstoexecuteiftheexpress
7、=xc<=RInsightThroughComputingSayingtheOppositexcisintheinterval[L,R]ifL<=xcandxc<=Rxcisnotintheinterval[L,R]ifxc8、9、(R10、=-b/2;if(L<=xc)&&(xc<=R)disp(‘Yes’)elsedisp(‘No’)endInsightThroughComputingTheif-elseConstructifendelsebooleanexpressionCommandstoexecuteiftheexpressionifTRUECommandstoexecuteiftheexpress
8、
9、(R10、=-b/2;if(L<=xc)&&(xc<=R)disp(‘Yes’)elsedisp(‘No’)endInsightThroughComputingTheif-elseConstructifendelsebooleanexpressionCommandstoexecuteiftheexpressionifTRUECommandstoexecuteiftheexpress
10、=-b/2;if(L<=xc)&&(xc<=R)disp(‘Yes’)elsedisp(‘No’)endInsightThroughComputingTheif-elseConstructifendelsebooleanexpressionCommandstoexecuteiftheexpressionifTRUECommandstoexecuteiftheexpress
此文档下载收益归作者所有
点击更多查看相关文章~~