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

Introduction to Computer Programming

  • Upload
    luther

  • View
    46

  • Download
    3

Embed Size (px)

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. REVIEW Definition of Conditional Statements - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Computer Programming

Introduction to Computer

Programming

School of Computer and Information Science

Southwest Forestry University

2012.9

Page 2: Introduction to Computer Programming

Danju LvAutumn semester , 2012

Chapter 3 Conditional Statements

Page 3: Introduction to Computer Programming

REVIEWDefinition of Conditional Statements Logical judgment and conditional

expression if statementelse statementelif statementMix of conditionals

Chap3 Conditional Statements

条件语句的定义逻辑判断与条件表达式单分支语句双分支语句多分支语句条件嵌套语句

Page 4: Introduction to Computer Programming

Chap3 conditional statements条件语句是根据条件表达式的值是 True/not zero还是 False/zero 做出决策,控制执行的代码块。

4 个要点:表达式 值控制代码块

4 key pointsExpression Value of the Expression

Control Block of Code/suit

Page 5: Introduction to Computer Programming

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.

Page 6: Introduction to Computer Programming

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

开始

输出圆面积

结束

计算圆面积

输入半径

半径不小于 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

Page 7: Introduction to Computer Programming

1. When input r is negative number, what’s the result? What we will 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

Page 8: Introduction to Computer Programming

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_suiteelse: expr_false_suite

Page 9: Introduction to Computer Programming

X=2

Understanding of else statement请画出以下程序的框图结构,该程序的执行路线如

何,结果是?若程序的 x=2 替换为 x=-2, 程序的执行路线又如何,结果?

x = 2if x > 0: y = 1+2*xelse: y = 0print('y=', y)

Falsex>0

True

y=1+2*x y=0

print(‘y=’,y)

Page 10: Introduction to Computer Programming

Eg3_3. input an integer from the keyboard, if it is even, then print “even” ; if it is odd, then print "odd".

Eg. Of Else Statements

[Analyze:] An integer is either an even number or an odd number, so it is an else statement case The variable settings 变量的设置 The conditional expression 条件表达式的表示 Choose the right conditional statements 条件结构的确

Page 11: Introduction to Computer Programming

Block diagram

Falsex被 2 整除 ?True

start

end

Get num. from the keyboard

输出“偶数” 输出“奇数”

1. Use input(): get a integer2. Use “=“ save the num. in x variable from the keyboard

1. 使用 input(): 获取一个整数2. 使用“ =“ 将从键盘获取的数存于变量 X

X%2==0 or x%2 is 0

Use print() show the result

Page 12: Introduction to Computer Programming

program

Falsex被 2 整除 ?True

start

end

Get num. from the keyboard

输出“偶数” 输出“奇数”

#Exp3_3.py x=eval(input(' 请输入一个整数: '))if x%2==0: #judge an even print(‘ 偶数 ')else: print(' 奇数 ')

程序运行结果:请输入一个整数: 2偶数

if expression: expr_true_suiteelse: expr_false_suite

Page 13: Introduction to Computer Programming

【 eg3-4】 Design a program for a telecommunications company interview job seekers. The program is to tell the job seekers whether meet the required educational conditions. If Job seekers meet one of the following conditions ,they will receive an interview notice, if not , they will receive a rejection notice. The educational conditions are the following:

Eg. Of Else Statements

为某电信公司面试求职者设计一程序。该程序是给满足某些教育条件的求职者提供面试机会。满足如下条件的求职者会接到面试通知:

Page 14: Introduction to Computer Programming

(1) at least 25 years of age , the electronic information engineering graduates.

(2) Key Universities , Electronic Information Engineering graduates.

(3) Under 28-year-old , computer science graduate

Meet One of the above conditions can gain an interview

Educational conditions

( 1 ) 25 岁以上,电子信息工程专业毕业生。( 2 )重点大学电子信息工程专业毕业生。( 3 ) 28 岁以下,计算机专业毕业满足以上条件之一即可获得面试机会

Page 15: Introduction to Computer Programming

Eg of else statement

【分析】:变量的设置, 选择表达式, 分支语句1.分析条件可知公司面试条件涉及 3 个方面:年龄、所学专业和毕业学校。为此设定 3 个变量( age, subject和 college )分别表示;2.选择条件设置: 3 个条件只需满足其中一个即可,其逻辑关系应为 : 或者 or ;而在每一个条件里涉及的方面又应为“与 and” 的逻辑关系。3.分支判断:若满足如上复合条件,则显示“恭喜,你已获得我公司的面试机会”,否则显示“抱歉,你未达到面试要求”。

Page 16: Introduction to Computer Programming

Block of Diagram开始

age > 25 and subject=="电子信息工程" or

college=="重点" and subject=="电子信息工程" or

age<=28 and subject=="计算机"

True

Fal se

age=24subject="计算机"college="非重点"

显示"恭喜,你已获得我公司的面试机!"

显示"抱歉,你未达到面试要求"

结束

Page 17: Introduction to Computer Programming

Program

# 程序:#Exp3-6.pyage=24subject=" 计算机 "college=" 非重点 "if (age > 25 and subject==" 电子信息工程 ")\or (college==" 重点 " and subject==" 电子信息工程 " )\or (age<=28 and subject==" 计算机 "):

print(“ 恭喜,你已获得我公司的面试机会 !")else:

print(“ 抱歉,你未达到面试要求“ )

程序运行结果:恭喜,你已获得我公司的面试机会 !

( 1 ) 25 岁以上,电子信息工程专业毕业生。( 2 )重点大学电子信息工程专业毕业生。( 3 ) 28 岁以下,计算机专业毕业满足以上条件之一即可获得面试机会

Page 18: Introduction to Computer Programming

Think

例题中,作为面试者的条件是在源程序中进行设定的,如何获取求职者三方面的信息呢?可以在程序中设置 3 个问题:哪所学校毕业?学的专业是什么?求职者的年龄?1. 毕业的学校是重点院校吗?( 1/2 ): 1. 重点 2. 非重点2. 学的专业是什么?( 1/2/3 ):

1. 电子信息工程; 2. 计算 3. 其它3. 你的年龄?

此时程序应如何改动呢?

Page 19: Introduction to Computer Programming

Python's design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in code that is syntactically correct. One of those such problems is the (in)famous "dangling else" problem, a semantic optical illusion.

“Dangling else” avoidance

Page 20: Introduction to Computer Programming

Please draw diagram for those programs. And tell the results of those programs under the balance is negative or positive.

Diagram

Page 21: Introduction to Computer Programming

Syntax of elif statementif expression1:

expr1_true_suite

elif expression2:

expr2_true_suite

….elif expressionN:

exprN_true_suite

else:

none_of_the_above_suite

elif statement

It allows one to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.

Page 22: Introduction to Computer Programming

Block of diagram of elif statement

分支结束

表达式1

Ture_expr1_sui t

Ture

False

表达式1

T

表达式2

Ture

表达式n

Ture

False……

False

……

Ture_expr2_sui t

Ture_exprN_sui t

None of the above_sui t

if expression1:expr1_true_suite

elif expression2:expr2_true_suite

….elif expressionN:

exprN_true_suiteelse:

none_of_the_above_suite

arbitrary number of elif

Page 23: Introduction to Computer Programming

[3-5]Grade student achievement by computer (Fail, Pass, intermediate, Good, Excellent). Its criteria for the classification :

Fail is less than 60; 60 to 70 are classified as Pass, 70 to 80 are divided into Intermediate80 to 90 divided into Benign 90 to 100-Excellent. Finally, display its class information

Eg. Of elif Statement

Page 24: Introduction to Computer Programming

1. Variable settingWhere to get date, how to save date

2. ExpressionComparison expr.

3. ConditionsElif statement

Analyze

Page 25: Introduction to Computer Programming

Diagram of Example

程序结束

Score<0 or score >100

无效成绩

Ture

False

T

Score<60

不及格

Ture

Score<70

及格

Ture

中等

Score<80

Score<90

良好 优秀

键盘输入score

开始

Ture

Ture

False

False

False

False

Page 26: Introduction to Computer Programming

Program 程序score=eval(input(' 请输入你的成绩( 0 ~100 ): '))if score<0 or score>100: print(' 无效的成绩 ')elif score<60: print(' 不及格 ')elif score<70: print(' 及格 ')elif score<80: print(' 中等 ')elif score<90: print(' 良好 ')else: print(' 优秀 ')程序运行结果:请输入你的成绩( 0 ~ 100 ): 88良好

Page 27: Introduction to Computer Programming

选择结构的嵌套 在某一个分支的语句块中,需要进行新的分支。这种结构你为选择结构的嵌套。嵌套的形式如下:if 表达式 1: # 语句块 1…

if 表达式 11:语句块 11…

else:语句块 12

…else: 语句块 2

Page 28: Introduction to Computer Programming

选择程序举例【例 3-6 】选择结构的嵌套问题。购买地铁车票的规定如下:乘 1-4 站, 3 元 / 位;乘 5-9 站, 4 元 / 位;乘 9站以上, 5 元 / 位。 输入人数、站数,输出应付款。

分析:需要进行两次分支。根据“人数 <=4” 分支一次,表达式为假时,还需要根据“人数 <=9” 分支一次。

Page 29: Introduction to Computer Programming

程序框图

图 3-9 计算乘地铁应付款

Falsem<=4

True

开始

输出应付款

结束

输入人数 n 、站数m

m<=9

pay=4*n

pay=5*nTrue

False

pay=3*n

Page 30: Introduction to Computer Programming

程序

#Exp3_5.pyn, m=eval(input(' 请输入数,站数 :'))if m<=4: pay=3*nelse: if m<=9: pay=4*n else: pay=5*nprint(' 应付款: ', pay)

输入及程序运行结果:请输入数,站数 :3,5应付款: 12

Page 31: Introduction to Computer Programming

Conditional statements If statementElse statementElif statementMix/nested of conditional statementDeeply understand control structureCore: the value of the expr. control the

execution of suits/blocks of code.

Chapter Summarizes