分支结构PPT-复习课件

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

14
2.16 switch Multiple-Selection Structure
15
活动图: necessary to exit switch
16
switch Multiple-Selection Structure (多分支结构) – 一旦条件满足, 后继的判断均被忽略(skipped)
4
2.2
Algorithms(续)
通常意义上讲,算法是指解决问题的一种方法或一个过程. 严格地讲,用计算机解决问题的算法是由若干条指令组成的有 穷序列,且满足下述性质:
– 输入: – 输出: 有零个或多个,由外部提供的量作为算法的输入; 至少产生一个量作为输出;
– 可执行性:每条指令都必须是能够被计算机识别的; – 确定性: 每条指令必须是无歧义的;
问题:输入一名学生的百分制成绩,打印该成绩相对应 的五分制的成绩(按字母打印). 伪代码:
switch (grade/10) { case 10: 学生的grade除10后等于 10 就Print "A" case 9: case 8: case 7: 学生的grade除10后等于 9 就Print "A" 学生的grade除10后等于 8 就Print "B" 学生的grade除10后等于 7 就Print "C"
– Actions(动作) to be executed – Order(次序) to be executed
Activity diagram ( 活动图 ) and Pseudocode ( 伪代码 ) (§2.3)
3
2.3
Pseudocode(伪代码)
Pseudocode
– 是一种人工的,非正式的语言(Artificial, informal language ) – 目的:辅助程序员设计算法(used to develop algorithms) – 但是,它是不可执行的( Not executed )on computers – 作用:帮助程序员思考(think out) program before coding – 特点:仅由可执行( executable )运算的语句构成,这 样很容易从伪代码转换到(convert into) C++ program – So,No need to declare variables(变量声明)
9
2.6
if…else Double-Selection Structure
10
问题:如果学生成绩及格,打印"Passed",否则打印"Failed". Flowchart of pseudocode statement(伪代码语句的活动图)
11
Pseudocode
如果 学生的成绩(grade)大于或等于 60分 打印输出"Passed" 否则 // 判断条件为false 或 0 打印输出"Failed"
9 57
(常数) 整数,单字符,字符串: 9 :一个整数. 字符. "9":在""之间有一个字符序列 组成的字符串.
ቤተ መጻሕፍቲ ባይዱ
(变量) 数据类型 int
' 9 ':在''之间只能有一个(数字) char char
9
\0
字符串中止符
C++ Keywords Keywords common to auto continue enum if short switch volatile C++ only keywords asm delete inline private static_cast try wchar_t the C and C++ break default extern int signed typedef while bool dynamic_cast mutable protected template typeid programming char double for register static unsigned languages const else goto return struct void
– 有穷性: 每条指令的执行次数是有限的,计算机执行每条指令的时 间也是有限的.
5
2.4
Control Structures
Sequential execution(顺序执行) Transfer of control(控制转移) 从C++程序语言的语法规则角度:3 种控制结构
– Sequence structure(顺序结构) – Selection structures(选择结构)
参见教材 图2.2

if, if/else, switch
– Repetition structures(循环结构)
while, do/while, for
6
2.5
if Single-Selection Structure: (单选择结构) Single - entry / single - exit
7
问题:如果学生的成绩及格,打印"Passed". Flowchart of pseudocode statement(伪代码语句的活动图)
18
case do float long sizeof union
catch class explicit false namespace new reinterpret_cast public this throw typename using
const_cast
friend operator true virtual
int main() { int grade; cout << "Enter student's grade: "<<endl; cin >>grade; if ( grade >= 60 ) cout << "Passed\n"<<endl; return 0; }
语法格式: if(条件表达式) 一条语句; 下一条语句;
C++ statement : cout << ( grade >= 60 ? "Passed" : "Failed" );
Condition Value if true Value if false
分支语句结构的小结
13
在分支语句的条件表达式中,经常使用的是关系运算符,逻辑运算 符,逻辑运算符与关系运算符联合.
2.2
Algorithms(算法)
2
Computing problems(计算问题)
– Solved by executing a series of actions(一系列动作) in a specific order (某种次序)
Algorithm: a procedure determining(解决问题的过程)
if语句 一个分支语句结构 if(条件表达式) 一条语句; 语句; 或 if(条件表达式) { ……; //语句块 } 语句;
if(条件表达式) { ……;// T } else { ……;// F } 语句;
if…else语句 两个分支语句结构
if,if…else的嵌套语句结构 多分支语句结构 第1种:if…else if…else 第2种:if…else + if…else 第3种:if…else + if 第4种: if + if…else 第5种:前4种 + 逻辑运算符
8
Pseudocode :
如果 学生的成绩(grade)大于或等于60分,输出"Passed";
C++ statements: if ( grade >= 60 ) cout << "Passed";
C++ program // A decision of student's grade (完整的C++程序) #include <iostream.h>
1
Objectives
To understand basic problem-solving techniques To be able to develop algorithms(算法) through the process of top-down, stepwise refinement (自顶向下求精法) To be able to use the if, if/else, switch selection structures (选择 结构 --- 即:分支语句) To be able to use the while, do/while, for repetition structure (循环结构 --- 即:循环语句) To be able to use the break, continue control statements (即: 跳转语句) To understand counter-controlled repetition (计数器控制的循 环) and sentinel-controlled repetition (标记控制的循环) To be able to use the increment (自增), decrement (自减), assignment operator (赋值运算符) and logic operators (逻辑 运算符)
case 6: 学生的grade除10后等于 6 就Print "D" default : 其余则Print "F" }
17
switch的判断依据是控制(条件)表达式的值: 整数常数 ,字符常数,枚举常量 switch的控制(条件)表达式只能是: 整型表达式, 字符型表达式,枚举型表达式.
(变量) 在内存中的存储形式:
语法格式:
C++ statement
if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; if( 条件表达式 ) 一条语句; else 另一条语句; 下一条语句;
12
2.6
if/else Selection Structure
Ternary conditional operator (?:)(条件运算符) – 三元运算符
相关文档
最新文档