《C++程序设计》(英文版) Chapter 9 Topics

Programming in c++ Additional Control Structures Dale/eems/Headington
1 Additional Control Structures

Programming in C++ Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping s For Statement for Looping % Using break and continue Statements To be able to choose the most appropriate looping statement for a given problem
2 Chapter 9 Topics ❖Switch Statement for Multi-way Branching ❖Do-While Statement for Looping ❖For Statement for Looping ❖Using break and continue Statements ❖To be able to choose the most appropriate looping statement for a given problem

Programming in C++ Switch Statement Is a selection control structure for multi-way branching SYNTAX switch( IntegralExpression case Constant Statement 1 ∥ optional case Constant2 Statement 2. ∥ optional default ∥ optional Statement n ∥ optional
3 Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement 1; // optional case Constant2 : Statement 2; // optional . . . default : // optional Statement n; // optional }

Programming in C++ float weightin Pounds =165.8; char weightUnit M/ user enters letter for desired weightUnit switch( weightUnit case“P casep. cout≤ weightln Pounds≤≤“ pounds“≤<endl break case case‘o3 cout≤<16.0* weightIn Pounds≤“ ounces“≤<endl; break case“K case“k cout < weightIn Pounds/22≤≤“klos“<end; break case‘G case ' g cout≤<454.0* weightin Pounds<“ grams“≤<end break default cout≤<“ That unit is not handled!“≤endl; break 4
4 float weightInPounds = 165.8 ; char weightUnit ; . . . // user enters letter for desired weightUnit switch ( weightUnit ) { case ‘P’ : case ‘p’ : cout << weightInPounds << “ pounds “ << endl ; break ; case ‘O’ : case ‘o’ : cout << 16.0 * weightInPounds << “ ounces “ << endl ; break ; case ‘K’ : case ‘k’ : cout << weightInPounds / 2.2 << “ kilos “ << endl ; break ; case ‘G’ : case ‘g’ : cout << 454.0 * weightInPounds << “ grams “ << endl ; break ; default : cout << “That unit is not handled! “ << endl ; break ; }

Programming in C++ Switch Statement o the value of IntegralExpression (of char, short, int, long or enum type) determines which branch is executed o case labels are constant( possibly named integral expressions, Several case labels can precede a same statement
5 Switch Statement ❖the value of IntegralExpression (of char, short, int, long or enum type ) determines which branch is executed ❖case labels are constant ( possibly named ) integral expressions. Several case labels can precede a same statement

Programming in C++ Switch (letter) icase X: Statement 1 break case 'L caseM. Statement2 break case 'S’: Statement3; break default Statement Statements
6 Switch(letter) {case ‘X’ : Statement1; break; case ‘L’ : case ‘M’: Statement2; break; case ‘S’ : Statement3; break; default : Statement4; } Statement5;

Programming in C++ >In this example, letter is the switch expression >The statement means %If letter is X, execute Statement1 and continue with Statements If letter is" L' or 'M, execute Statement2 and continue with Statements %If letter isS, execute Statement and continue with Statements %If letter is none of the characters mentioned execute Statement and continue with Statements >The Break statement causes an immediate exit from the Switch statement
7 ➢In this example, letter is the switch expression. ➢The statement means ❖If letter is ‘X’,execute Statement1 and continue with Statement5. ❖If letter is ‘L’ or ‘M’, execute Statement2 and continue with Statement5. ❖If letter is ‘S’, execute Statement3 and continue with Statement5. ❖If letter is none of the characters mentioned, execute Statement4 and continue with Statement5. ➢The Break statement causes an immediate exit from the Switch statement

Programming in C++ Control in Switch Statement o control branches to the statement following the case label that matches the value of IntegralExpression. Control proceeds through all remaining statements including the default, unless redirected with break o if no case label matches the value of IntegralExpression, control branches to the default label(if present)--otherwise control passes to the statement following the entire switch statement .g forgetting to use break can cause logical errors
8 Control in Switch Statement ❖ control branches to the statement following the case label that matches the value of IntegralExpression. Control proceeds through all remaining statements, including the default, unless redirected with break ❖ if no case label matches the value of IntegralExpression, control branches to the default label(if present)——otherwise control passes to the statement following the entire switch statement ❖ forgetting to use break can cause logical errors

Programming in C++ Switch(grade) //Wrong Version case ' A case 'B: cout <<Good Work case 'C:cout<<“ Average Work” case 'D case 'F. cout < Poor Work numberIn trouble++ default: cout < grade < is not a valid letter grade
9 Switch (grade) // Wrong Version { case ‘A’ : case ‘B’ : cout <<“Good Work”; case ‘C’ : cout <<“Average Work”; case ‘D’ : case ‘F’: cout <<“Poor Work”; numberInTrouble++; default : cout << grade <<“is not a valid letter grade.” }

Programming in C++ )If grade is A, the resulting output is this Good WorkAverage WorkPoor WorkA is not a valid letter grade Remember After a branch is taken to a specific case label, control proceeds sequentially until either a Break statement or the end of the Switch statement occurs 10
10 ➢If grade is ‘A’, the resulting output is this: Good WorkAverage WorkPoor WorkA is not a valid letter grade. ➢Remember: After a branch is taken to a specific case label, control proceeds sequentially until either a Break statement or the end of the Switch statement occurs
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++程序设计》(英文版) Chapter 8 Topics.ppt
- 《C++程序设计》(英文版) Chapter 7 Topics.ppt
- 《C++程序设计》(英文版) Chapter 6 Topics.ppt
- 《C++程序设计》(英文版) Chapter 5 Topics.ppt
- 《C++程序设计》(英文版) Chapter 4 Topics.ppt
- 《C++程序设计》(英文版) Chapter 3 Topics.ppt
- 《C++程序设计》(英文版) Chapter 2 Topics.ppt
- 《C++程序设计》(英文版) Chapter 1 Topics.ppt
- 《网络互连技术教程》第9章 广播.ppt
- 《网络互连技术教程》第8章 用户数据报协议—UDP.ppt
- 《网络互连技术教程》第7章 CMP和网络状态.ppt
- 《网络互连技术教程》第6章 地址解析.ppt
- 《网络互连技术教程》第5章 子网与超网.ppt
- 《网络互连技术教程》第4章 网络互连协议——IP.ppt
- 《网络互连技术教程》第3章 网络接入技术.ppt
- 《网络互连技术教程》第2章 网络互连体系.ppt
- 《网络互连技术教程》第1章 网络的历史与发展.ppt
- 《网络互连技术教程》第12章 应用服务.ppt
- 《网络互连技术教程》第11章 域名服务系统—DNS.ppt
- 《网络互连技术教程》第10章 传输控制协议—7CP.ppt
- 《C++程序设计》(英文版) Chapter 10 Topics.ppt
- 《C++程序设计》(英文版) Chapter 11 Topics.ppt
- 《C++程序设计》(英文版) Chapter 12 Topic.ppt
- 《C++程序设计》(英文版) Chapter 13 Topics.ppt
- 《C++程序设计》(英文版) Chapter 14 Topics.ppt
- 《C++程序设计》(英文版) Chapter 15 Topics.ppt
- 《Java编程技术基础》第一章 面向对象原理与实现.ppt
- 《Java编程技术基础》第二章 Java的实现基础.ppt
- 《Java编程技术基础》第二章习题.doc
- 《Java编程技术基础》第三章 类与对象(一).ppt
- 《Java编程技术基础》第三章习题.doc
- 《Java编程技术基础》第四章 类与对象(二).ppt
- 《Java编程技术基础》第四章习题.doc
- 《Java编程技术基础》第五章 异常与垃圾收集.ppt
- 《Java编程技术基础》第五章习题.doc
- 《Java编程技术基础》第十一章 Java集合框架.ppt
- 《Java编程技术基础》第十一章习题.doc
- 《Java编程技术基础》第二章 JDBC.ppt
- 武汉外语外事职业学院:《VisualBasic语言程序设计教程》第二版 第14章 图形处理.ppt
- 武汉外语外事职业学院:《VisualBasic语言程序设计教程》第二版 第3章 窗体.ppt