Introduction to Computer Programming School of Computer and

  • Slides: 28
Download presentation
Introduction to Computer Programming School of Computer and Information Science Southwest Forestry University 2012.

Introduction to Computer Programming School of Computer and Information Science Southwest Forestry University 2012. 9

Chapter 3 Conditional Statements Danju Lv Autumn semester , 2012

Chapter 3 Conditional Statements Danju Lv Autumn semester , 2012

Chap 3 Conditional Statements In this chapter: Ø Definition of Conditional Statements Ø Logical

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

ØDef. of Conditional Statements This chapter describes a type of control statements in Python

Ø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/零做出决策,控制代码块的执行。

Chap 3 conditional statements 条件语句是根据条件表达式的值是True/not zero 还是False/zero做出决策,控制执行的代码块。 由表达式的值控制执行的代码块 4 key points Expression Value of

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

1. Expression Def. of Expression: A typical expression is generally composed of: operator and

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

Exp. of expresstion 1. Math Expressions: 3+2 7%3 3**2 2. Comparison Expressions: 3<6 2<>7

Exp. of expresstion 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 expression Tell the values of the following expressions: 判断下列逻辑表示式的值 Expr. : 2

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

Conjunction in cond. expression When a judgment need to consider two or more of

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. 当做一个判断时需考虑两个或两个以上 的条件/因素时,此时需要对条件进行合理的逻 辑组合运算。

ØValues of the expressions 表达式 Core: Any value is an expression! 的值 核心 :有值便是表达式!

ØValues of the expressions 表达式 Core: Any value is an expression! 的值 核心 :有值便是表达式! 如: 2, “abc” 在python中对于所有对象都做了True, False的映射 可用bool函数来决定任何对象、表达式的布尔值 对象类型 True False dict 除alse外 {} list [] string ‘’ int 0 double, float 0. 0 class None

3. control:conditional statements 选择控制 l if statement if 条件语句:the syntax of if statement: if

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

if 单分支语句 if expression: expr_true_suite The suite of the if clause, expr_true_suite, will be

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

双分支语句 else statement Syntax of else statement: if 表达式 : if expression: expr_true_suite Ture语句块

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