Chapter 2 - Control Structures.ppt
《Chapter 2 - Control Structures.ppt》由会员分享,可在线阅读,更多相关《Chapter 2 - Control Structures.ppt(39页珍藏版)》请在麦多课文档分享上搜索。
1、1,Chapter 2 - Control Structures,Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure 2.8 Formulating Algorithms: Case Study 1: Counter-Controlled Repetition) 2.9 Formulating Algorithms
2、 with Top-Down, Stepwise Refinement: Case Study 2: Sentinel-Controlled Repetition) 2.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3: Nested Control Structures),2,2.1 Introduction,Before writing a program Have a thorough understanding of problem Carefully plan your approac
3、h for solving it While writing a program Know what “building blocks” are available Use good programming principles,3,2.2 Algorithms,Computing problems Solved by executing a series of actions in a specific order Algorithm a procedure determining Actions to be executed Order to be executed Example: re
4、cipe Program control Specifies the order in which statements are executed,4,2.3 Pseudocode,Pseudocode Artificial, informal language used to develop algorithms Similar to everyday English Not executed on computers Used to think out program before coding Easy to convert into C+ program Only executable
5、 statements No need to declare variables,5,2.4 Control Structures,Sequential execution: statements executed in order cout “Welcome “; cout “to “; cout “C+“; Transfer of control: Next statement executed not next one in sequence cout “Welcome“; cout “ to“; if (course=105)cout “ C!“; else if (course=18
6、1)cout “ C+! “; else / Print error message and exitcerr “Unknown course“;exit(-1);,6,2.4 Control Structures,3 control structures (Bohm and Jacopini) are sufficient to express any algorithm Sequence structure Programs executed sequentially by default Selection structures if, if/else, switch Repetitio
7、n structures while, do/while, for,gotos are bad, mmm kay?,“Go To Statement Considered Harmful“ Edsger W. Dijkstra, CACM Mar 1968 http:/www.acm.org/classics/oct95/ Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148,7,Example of “goto“ statement,cout “Welcome“; cout “ to“; if (course =
8、 181) goto greet181; if (course = 105) goto greet105; cerr “nError: Unknown Course“ endl; return -1; / exit with error status greet181: cout “ C+!“ endl; goto endProgram; greet105: cout “ C!“ endl; goto endProgram; endProgram: cout “Goodbye for now“ endl; return 0; / exit successfully.,cout “Welcome
9、“; cout “ to“; if (course = 181) cout “ C+!“ endl; else if (course = 105) cout “ C!“ endl; else cerr “nError: Unknown Course“ endl; return -1; / exit with error status cout “Goodbye for now“ endl; return 0; / exit successfully.,cout “Welcome“; cout “ to“; if (course = 181) goto greet181; if (course
10、= 105) goto greet105; cerr “nError: Unknown Course“ endl; return -1; / exit with error status greet181: cout “ C+!“ endl; goto endProgram; greet105: cout “ C!“ endl; goto endProgram; endProgram: cout “Goodbye for now“ endl; return 0; / exit successfully.,8,Dijkstra Letter Quotes,.the quality of prog
11、rammers is a decreasing function of the density of go to statements in the programs they produce. .the go to statement should be abolished from all “higher level“ programming languages (i.e. everything except, perhaps, plain machine code). we should . shorten the conceptual gap between the static pr
12、ogram and the dynamic process, to make the correspondence between the program (spread out in text space) and the process (spread out in time) as trivial as possible. The go to statement as it stands is just too primitive; it is too much an invitation to make a mess of ones program,Go To Statement Co
13、nsidered Harmful, Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148,Edsger W. Dijkstra 1930-2002,9,More Dijkstra Quotes,“Computer Science is no more about computers than astronomy is about telescopes.“ “A Programming Language is a tool that has profound influence on our thinking hab
14、its.“ “The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague.“ “Progress is possible only if we train ourselves to think about programs witho
15、ut thinking of them as pieces of executable code.“ “Program testing can best show the presence of errors but never their absence.“,10,2.4 C+ keywords,Cannot be used as identifiers or variable names,Keywords common to the C and C+ programming languages,C+ only keywords,11,2.4 Control Structures,Flowc
16、hart Graphical representation of an algorithm Special-purpose symbols connected by arrows (flowlines) Rectangle symbol (action symbol) Any type of action Oval (or circle) symbol Beginning or end (of a program, or a section of code) Single-entry/single-exit control structures (“no gotos“) Connect exi
17、t point of one to entry point of the next Control structure stacking,12,2.5 if Selection Structure,Selection structure: Choose among alternative courses of action If students grade is greater than or equal to 60Print “Passed” if ( grade = 60 ) cout “Passed“; Indenting makes programs easier to read C
18、+ ignores whitespace characters (tabs, spaces, etc.) Diamond symbol (decision symbol) Indicates decision is to be made Contains an expression that can be true or false Note: in C/C+, every expression has true/false value: zero implies false, nonzero implies true. Example: if ( 3 - 4) is interpreted
19、as “true“. if structure has single-entry/single-exit,13,2.6 if/else Selection Structure,if Performs action if condition true if/else Different actions if conditions true or falseif students grade is greater than or equal to 60 print “Passed” elseprint “Failed” if ( grade = 60 ) cout “Passed“; else c
20、out “Failed“;,14,2.6 Ternary conditional operator (?:),Three arguments: (condition, value if true, value if false)cout = 60 ? “Passed” : “Failed” );,15,Comparison,if/else if ( grade = 60 ) cout = 60 ? “Passed” : “Failed” );,16,2.6 if/else Selection Structure,Nested if/else structures One inside anot
21、her, test for multiple cases Once condition met, other statements skipped if students grade is greater than or equal to 90 Print “A” else if students grade is greater than or equal to 80 Print “B” else if students grade is greater than or equal to 70 Print “C” else if students grade is greater than
22、or equal to 60 Print “D” elsePrint “F”,17,2.6 if/else Selection Structure,Example if ( grade = 90 ) / 90 and above cout = 80 ) / 80-89 cout = 70 ) / 70-79 cout = 60 ) / 60-69 cout “D“; else / less than 60 cout “F“;,18,2.6 if/else Selection Structure,Compound statement Set of statements within a pair
23、 of bracesif ( grade = 60 ) cout “Passed.n“; else cout “Failed.n“; cout “You must take this course again.n“; Without braces, cout “You must take this course again.n“; always executed Block Set of statements within braces,19,2.6 Indentation Rules,Deitel/Deitel prefer: if ( grade = 60 ) cout = 60 ) co
24、ut “Passed.n“; else cout “Failed.n“; cout “You must take this course again.n“; Whatever you do, be consistent. (choose one way or other way, otherwise may lose points.),20,2.7 while Repetition Structure,Repetition structure Action repeated while some condition remains true while there are more items
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER2CONTROLSTRUCTURESPPT
