Alternate Version of STARTING OUT WITH C++ 4th Edition.ppt
《Alternate Version of STARTING OUT WITH C++ 4th Edition.ppt》由会员分享,可在线阅读,更多相关《Alternate Version of STARTING OUT WITH C++ 4th Edition.ppt(21页珍藏版)》请在麦多课文档分享上搜索。
1、Copyright 2004 Scott/Jones Publishing,Alternate Version of STARTING OUT WITH C+ 4th Edition,Chapter 5 Looping,Chapter 5 slide 2,Topics,5.1 The Increment and Decrement Operators 5.2 Introduction to Loops: The while Loop 5.3 Counters 5.4 Letting the User Control the Loop 5.5 Keeping a Running Total 5.
2、6 Sentinels 5.7 Using a Loop to Read Data From a File,Chapter 5 slide 3,5.1 The Increment and Decrement Operators,+ adds one to a variableval+; is the same as val = val + 1; - subtracts one from a variableval-; is the same as val = val 1;,Chapter 5 slide 4,Postfix Mode,val+ and val- return the old v
3、alue of the variable, then increment or decrement the variable It is this returned old value of the variable that is used in any other operations within the same statement,Chapter 5 slide 5,5.2 Introduction to Loops: The while Loop,Loop: part of program that may execute 0 or more times (i.e., it rep
4、eats)while loop formatwhile (expression) statement(s); If there is only one statement in the body of the loop, the can be omitted,Chapter 5 slide 6,How the while Loop Works,while (expression) statement(s); expression is evaluated if it is true, the statement(s) are executed, and then expression is e
5、valuated again if it is false, the loop is exited,Chapter 5 slide 7,while Loop Flow of Control,Chapter 5 slide 8,while Loop Example,int val = 5; while (val = 0) cout val “ “;val-; produces output:5 4 3 2 1 0,Chapter 5 slide 9,while Loop is a Pretest Loop,while is a pretest loop (expression is evalua
6、ted before the loop executes)If the expression is initially false, the statement(s) in the body of the loop are never executedIf the expression is initially true, the statement(s) in the body continue to be executed until the expression becomes false,Chapter 5 slide 10,Exiting the Loop,The loop must
7、 contain code to allow expression to eventually become false so the loop can be exited Otherwise, you have an infinite loop (i.e., a loop that does not stop) Example infinite loopx = 5;while (x 0) / infinite loop becausecout 0,Chapter 5 slide 11,5.3 Counters,Counter: variable that is incremented or
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ALTERNATEVERSIONOFSTARTINGOUTWITHC4THEDITIONPPT

链接地址:http://www.mydoc123.com/p-378219.html