1 / 28

Introduction to Computer Programming

School of Computer and Information Science Southwest Forestry University 2012.9. Introduction to Computer Programming. Chapter 3 Conditional Statements. Danju Lv Autumn semester , 2012. Chap3 Conditional Statements. In this chapter: Definition of Conditional Statements

mateo
Download Presentation

Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. School of Computer and Information Science Southwest Forestry University 2012.9 Introduction to Computer Programming

  2. Chapter 3 Conditional Statements Danju Lv Autumn semester , 2012

  3. Chap3 Conditional Statements • In this chapter: • Definition of Conditional Statements • Logical judgment and conditional expression • if statement • else statement • elif statement • Mix of conditionals • 条件语句的定义 • 逻辑判断与条件表达式 • 单分支语句 • 双分支语句 • 多分支语句 • 条件嵌套语句

  4. Def. of Conditional Statements This chapter describes a type of control statements in Python , conditional statement. Based on the value of the conditional expression ,True(non-zero ) or False (zero), the conditionals make decisions to control the execution of block of code. 条件语句是根据条件表达式的值是True/非零还是False/零做出决策,控制代码块的执行。

  5. Chap3 conditional statements 条件语句是根据条件表达式的值是True/not zero还是False/zero做出决策,控制执行的代码块。 由表达式的值控制执行的代码块 • 4 key points • Expression • Value of the Expression • Control • Block of Code/suit • 4个要点: • 表达式 • 值 • 控制 • 代码块

  6. 1. Expression • Def. of Expression: • A typical expression is generally composed of: operator and operand (object) • Common operators in conditional expressions • Math:+,-,*,/,// , %,  **,~, • Comparison: >, <, ==, != , <=, >= • Test:in , not in ,is , is not • Conjunction:and, or, not

  7. 1. Math Expressions: 3+2 7%3 3**2 2. Comparison Expressions: 3<6 2<>7 “abc”==“bcd” a=[1,2,3];b=[1,2,3]; a!=b 3. Test Expressions: a=[1,3,5]; 3 in a ; 5 not in a a=“abcdef”; b=a; a is b; a is not b Exp. of expresstion

  8. Conj. Operaters:and or not。 and 和 表示左边的运算、值或者对象为True,接着对右边求值。如果左边为False/zero,就停止运算并且输出结果False,不再继续运算。 or 或 表示对左边表达式求值,如果结果为False, 则继续对右边的表达式求值,得最后结果。如果左边表达式结果为True/not zero,则停止其后表达式求值,且最终结果为True。 not 非单目运算符 在需要在表达相反条件时,用not运算符。 Conjunction Operation 短路特性 Short circuit 运算特点:见False为False False at first False 运算特点:见True为True True at first True

  9. Tell the values of the following expressions: 判断下列逻辑表示式的值 Expr. :2 or 0, 2 and 0, 3 or 2, 3 and 2, not 2, not 0 Exp. of expression values:2, 0, 3, 2, False, True 逻辑运算表达式其值并非得到布尔型的结果

  10. Conjunction in cond. expression • When a judgment need to consider two or more of the conditions / factors, we should correctly use conj. operators to combine those conditionals . • 当做一个判断时需考虑两个或两个以上的条件/因素时,此时需要对条件进行合理的逻辑组合运算。

  11. 1. 成绩score在90~100之间? Exp. Of Conj. Expression逻辑组合表示举例 score>=90 and score<=100 2. 年龄age在25岁~30岁之间且专业subject是计算机或是电子信息工程专业? (age>=25 and age<=30 ) and (subject=="计算机" or subject=="电子信息工程专业”) 注意:表示两者关系相等用“==”, 而非“=”,初学者极易出错

  12. 1. 运算符的优先级 在一个表达式中出现多种运算符时,按照运算符的优先级高低依次进行运算。 注:表达式出现()时,运算级别是最高的,“=”级别最低 Mix of operation混合运算

  13. Mix of operation混合运算 2. 结合规律 通常,相同级别运算符按照从左到右顺序计算 注:=赋值运算是从右到左的结合。 a=b=c  a=(b=c)

  14. Core: Any value is an expression! 核心:有值便是表达式! 如:2, “abc” 在python中对于所有对象都做了True, False的映射 可用bool函数来决定任何对象、表达式的布尔值 Values of the expressions表达式的值

  15. 表达式的值若视为Boolean型,则表达式的值就只可能是True or False 在控制语句中表达式(条件表达式)的值就是视为Boolean型——逻辑判断 Values of expr. in control statements控制语句中表达式的值 由表达式的值控制执行的代码块

  16. The basis of the control: logic judgment 逻辑判断:体现了一种思维能力。 计算机有没有这种思维能力呢? 常用逻辑判断的类型 两个对象之间的关系(==,!=,<,>,>=,<=) 注: !=还可表示成:<> 成员测试 (in ,not in) 同一性测试(is,not is) 逻辑关系(not, and , or):常用于组合表达式中 3.control: logical judgment控制:逻辑判断

  17. if 条件语句:the syntax of if statement: if 表达式: ture语句块 False 表达式 True ture语句块 图3-1 单分支语句的执行方式 3. control:conditional statements选择控制 • if statement ifexpression: expr_true_suite 单分支语句,菱形框表示if,表达式放在框内,紧接菱形框的矩形框表示冒号后的true语句块。

  18. False 表达式 True true语句块 if 单分支语句 ifexpression: expr_true_suite 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 语句:若表达式为真/非零则执行冒号后的语句块,若表达式为假则跳过该语句块的执行。

  19. 试将以下程序用框图表示,并说明程序运行的结果试将以下程序用框图表示,并说明程序运行的结果 if 简单分支的执行情况分析 n = 1 x = 5 if x>0 : n = n+1 n=1 x=5 n=1 x=-5 False x>0 True n = 1 x = -5 if x>0 : n = n+1 n=n+1

  20. Indentation Python语言利用缩进表示语句块的开始和退出(Off-side规则),而非使用花括号或者某种关键字。 增加缩进表示语句块的开始(回车后自动完成/tab键) 减少缩进则表示语句块退出(backspace键) Indentation in ctrl. Statement of python控制程序中的缩进量

  21. 3-1两数大小比较,按从小到大的顺序输出。 简单分支举例 开始 a=3 b=5 if a>b: t=a a=b b=t print ("从小到大输出:") print (a, b) 程序运行结果: 从小到大输出: 3 5 初始化a、b False a>b True ? 交换a、b的值 输出a、b 结束 图3-2 按升序输出两个数

  22. 【例3-2】从键盘输入圆的半径,如果半径大于等于零则计算圆面积。【例3-2】从键盘输入圆的半径,如果半径大于等于零则计算圆面积。 开始 输入半径 False 半径不小于0 True 计算圆面积 输出圆面积 结束 简单分支举例 import math r=eval(input(“请输入圆的半径:”)) if r>=.0: s=math.pi*r**2 print("s=pi*r*r=",s) 结果验证: 请输入圆的半径:4 s=pi*r*r= 50.26548245743669

  23. 当输入r 为-2时,结果如何? 程序的输出可以改进吗? 如当输入r 为负值时,给出一个提示输入出错的信息,当输入r 为非负值时,计算其圆面积。 思考

  24. 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. 表达式 False True Ture语句块 False语句块 图3-4 双分支语句的执行方式 双分支语句 else statement ifexpression: expr_true_suite else: expr_false_suite

  25. 图3-4表示了双分支语句,菱形框表示if,表达式放在框内,左边的矩形表示“if表达式”冒号后的“True语句块”,右边的矩形表示“else”冒号后的“False语句块”,若表达式为真则执行Ture语句块,若表达式为假则执行False语句块。图3-4表示了双分支语句,菱形框表示if,表达式放在框内,左边的矩形表示“if表达式”冒号后的“True语句块”,右边的矩形表示“else”冒号后的“False语句块”,若表达式为真则执行Ture语句块,若表达式为假则执行False语句块。 表达式 False True Ture语句块 False语句块 双分支选择 else statement ifexpression: expr_true_suite else: expr_false_suite

  26. 请画出以下程序的框图结构,该程序的执行路线如何,结果是?若程序的x=2替换为x=-2,程序的执行路线又如何,结果?请画出以下程序的框图结构,该程序的执行路线如何,结果是?若程序的x=2替换为x=-2,程序的执行路线又如何,结果? x>0 False True y=1+2*x y=0 双分支程序的理解 X=2 x = 2 if x > 0: y = 1+2*x else: y = 0 print('y=', y) print(‘y=’,y)

More Related