introduction to computer programming
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Introduction to Computer
ProgrammingSchool of Computer and Information
Science Southwest Forestry University
2012.9
Danju LvAutumn semester , 2012
Chapter 3 Conditional Statements
In this chapter:Definition of Conditional Statements Logical judgment and conditional
expressionif statementelse statementelif statementMix of conditionals
Chap3 Conditional Statements
条件语句的定义逻辑判断与条件表达式单分支语句双分支语句多分支语句条件嵌套语句
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/ 零做出决策,控制代码块的执行。
Chap3 conditional statements条件语句是根据条件表达式的值是 True/not zero还是 False/zero 做出决策,控制执行的代码块。
4 个要点:表达式 值控制代码块
4 key pointsExpression Value of the Expression
Control Block of Code/suit
Def. of Expression:A typical expression is generally composed
of: operator and operand (object)
1. Expression
Common operators in conditional expressions
1.Math : + , - , * , / , // , % , **,~,2.Comparison : >, <, ==, != , <=, >=3.Test : in , not in , is , is not4.Conjunction : and, or, not
Exp. of expresstion1. 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
Conjunction Operation Conj. Operaters : and or not 。and 和 表示左边的运算、值或者对象为 True ,接着对右边求值。如果左边为 False/zero ,就停止运算并且输出结果 False ,不再继续运算。or 或 表示对左边表达式求值,如果结果为 False , 则继续对右边的表达式求值,得最后结果。如果左边表达式结果为True/not zero ,则停止其后表达式求值,且最终结果为 True。not 非 单目运算符在需要在表达相反条件时,用 not 运算符。
短路特性Short circuit
运算特点:见 False 为 False False at first False
运算特点:见 True 为 True True at first True
Exp. of expressionTell the values of the following expressions: 判断下列逻辑表示式的值Expr. :2 or 0, 2 and 0, 3 or 2, 3 and 2, not 2, not 0
values : 2 , 0 , 3 , 2, False, True
逻辑运算表达式其值并非得到布尔型的结果
When a judgment need to consider two or more of the conditions / factors, we should correctly use conj. operators to combine those conditionals .
当做一个判断时需考虑两个或两个以上的条件 / 因素时,此时需要对条件进行合理的逻辑组合运算。
Conjunction in cond. expression
Exp. Of Conj. Expression逻辑组合表示举例1. 成绩 score 在 90 ~ 100 之间?
2. 年龄 age 在 25 岁~ 30 岁之间且专业 subject是计算机或是电子信息工程专业?
score>=90 and score<=100
(age>=25 and age<=30 ) and (subject==" 计算机 " or subject==" 电子信息工程专业” )注意:表示两者关系相等用“ ==” , 而非“ = ” ,初学者极易出错
高Conj. Test Comp. mathnotAnd is, is not *,/,%,//Or in , not
in <>, ==,>=,<=,<, >
+, -
低 高
Mix of operation 混合运算1. 运算符的优先级 在一个表达式中出现多种运算符时,按照运算符的优先级高低依次进行运算。注:表达式出现()时,运算级别是最高的 ,“ =” 级别最低
2. 结合规律通常,相同级别运算符按照从左到右顺序计算注: = 赋值运算是从右到左的结合。a=b=c a=(b=c)
Mix of operation 混合运算
Values of the expressions 表达式的值Core: Any value is an expression!
核心:有值便是表达式 ! 如: 2 , “ abc” 在 python 中对于所有对象都做了 True, False 的映射可用 bool 函数来决定任何对象、表达式的布尔值对象类型 True False
dict 除 alse 外 {}list []string ‘’int 0double, float 0.0class None
Values of expr. in control statements 控制语句中表达式的值
表达式的值若视为 Boolean 型,则表达式的值就只可能是 True or False 在控制语句中表达式(条件表达式)的值就是视为 Boolean 型——逻辑判断
由表达式的值控制执行的代码块
3.control: logical judgment控制:逻辑判断The basis of the control : logic judgment逻辑判断:体现了一种思维能力。计算机有没有这种思维能力呢?常用逻辑判断的类型
两个对象之间的关系( == , != , < , > , >= , <=) 注: != 还可表示成: <>成员测试 ( in , not in )同一性测试( is , not is )逻辑关系( not , and , or ):常用于组合表达式中
3. control : conditional statements 选择控制
if 条件语句: the syntax of if statement :if 表达式 : ture 语句块
图 3-1 单分支语句的执行方式
True
False表达式ture 语句块
if expression: expr_true_suite
单分支语句,菱形框表示 if ,表达式放在框内,紧接菱形框的矩形框表示冒号后的 true 语句块。
if statement
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 expressionresults in a Boolean true value. Otherwise, execution resumes at the next statement following the suite.
if 简单分支的执行情况分析试将以下程序用框图表示,并说明程序运行的结果
True
False
x>0
n=n+1
n = 1x = 5if x>0 : n = n+1n = 1x = -5if x>0 : n = n+1
n=1x=5
n=1x=-5
Indentation in ctrl. Statement of python控制程序中的缩进量Indentation
Python 语言利用缩进表示语句块的开始和退出(Off-side规则),而非使用花括号或者某种关键字。增加缩进表示语句块的开始(回车后自动完成 /tab键 )减少缩进则表示语句块退出( backspace 键)
简单分支举例3-1 两数大小比较,按从小到大的顺序输出。
图 3-2 按升序输出两个数
True
Falsea>b
交换 a 、 b 的值
输出 a 、 b
开始
结束
a=3 b=5if a>b: t=a a=b b=tprint (" 从小到大输出: ")print (a, b)
程序运行结果:从小到大输出:3 5
初 始 化a 、 b
简单分支举例【例 3-2】从键盘输入圆的半径,如果半径大于等于零则计算圆面积。
True
开始
输出圆面积
结束
计算圆面积
输入半径
半径不小于 0False
import math r=eval(input(“请输入圆的半径:” ))if r>=.0: s=math.pi*r**2 print("s=pi*r*r=",s)
结果验证:请输入圆的半径: 4s=pi*r*r= 50.26548245743669
思考当输入 r 为 -2 时,结果如何?程序的输出可以改进吗?
如当输入 r 为负值时,给出一个提示输入出错的信息 , 当输入 r 为非负值时,计算其圆面积。
双分支语句 else statementSyntax 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_suiteelse: expr_false_suite
双分支选择 else statement
图 3-4 表示了双分支语句,菱形框表示 if ,表达式放在框内,左边的矩形表示“ if 表达式”冒号后的“ True 语句块”,右边的矩形表示“ else” 冒号后的“ False 语句块”,若表达式为真则执行 Ture语句块,若表达式为假则执行 False 语句块。
if expression: expr_true_suiteelse: expr_false_suite
False表达式True
Ture 语句块 False 语句块
双分支程序的理解请画出以下程序的框图结构,该程序的执行路线如何,结果是?若程序的 x=2替换为 x=-2, 程序的执行路线又如何,结果?
x = 2if x > 0: y = 1+2*xelse: y = 0print('y=', y)
X=2
Falsex>0
True
y=1+2*x y=0
print(‘y=’,y)