当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PPT,文档页数:50,文件大小:3.1MB,团购合买
Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements
点击下载完整版文档(PPT)

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

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共50页,可试读17页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有