IntroductiontoComputerProgramming.pptVIP

  1. 1、本文档共47页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  5. 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  6. 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  7. 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  8. 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
IntroductiontoComputerProgramming

if 单分支语句 if expression: expr_true_suite If 语句:若表达式为真/非零则执行冒号后的语句块,若表达式为假则跳过该语句块的执行。 True False 表达式 true语句块 The suite of the if clause, expr_true_suite, will be executed only if the above conditional expression results in a Boolean true value. Otherwise, execution resumes at the next statement following the suite. if 简单分支的执行情况分析 试将以下程序用框图表示,并说明程序运行的结果 True False x0 n=n+1 n = 1 x = 5 if x0 : n = n+1 n = 1 x = -5 if x0 : n = n+1 n=1 x=5 n=1 x=-5 简单分支举例 【例3-1】两数大小比较,按从小到大的顺序输出。 图3-2 按升序输出两个数 True False ab 交换a、b的值 输出a、b 开始 结束 a=3 b=5 if ab: t=a a=b b=t print (从小到大输出:) print a, b 程序运行结果: 从小到大输出: 3 5 初始化a、b Exp of if statement 【例3-2】 Input the radius of the circle from the keyboard, if the radius is larger than or equal to zero , then calculate the area of circle. True 开始 输出圆面积 结束 计算圆面积 输入半径 半径不小于0 False import math r=input(请输入圆的半径:) if r=1e-6: s=math.pi*r**2 print s=pi*r*r=,s #print(s=pi*r*r=+%.2f%s) 结果验证: 请输入圆的半径:4 s=pi*r*r= 50.279 简单分支举例 【例3-2】从键盘输入圆的半径,如果半径大于等于零则计算圆面积。 True 开始 输出圆面积 结束 计算圆面积 输入半径 半径不小于0 False import math r=input(‘请输入圆的半径:’) if r=0: s=math.pi*r**2 print(s=pi*r*r=,s) 结果验证: 请输入圆的半径:4 s=pi*r*r= 50.26548245743669 When input r is negative number, what’s the result? What will we see on the screen? 2. The output of the program could be improved? Such as when the input r is negative, give a prompt input error message, when the input r is non-negative, calculate the area of the circle. Think 思考 当输入r 为-2时,结果如何? 程序的输出可以改进吗? 如当输入r 为负值时,给出一个提示输入出错的信息,当输入r 为非负值时,计算其圆面积。 双分支语句 else statement Syntax of else statement: if 表达式 : Ture语句块 else : False语句块 The else statement identifies a block of code(expre false suite) to be executed if the conditional expression of the if statement resolves to a false Boolean value. 图3-4 双分支语句的执行方式 False 表达式 True Ture语句块 False语句块 if expression: expr_true_suite else: expr_false_suite 双分支程序的理解 请画出以下程序的框图结构,该程序的执行路线如何,结果是?若程序的x=2替换为x

文档评论(0)

118books + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档