CC++:从基础语法到优化策略 (3).pdf

上传人:奉*** 文档编号:67730948 上传时间:2022-12-26 格式:PDF 页数:36 大小:4.08MB
返回 下载 相关 举报
CC++:从基础语法到优化策略 (3).pdf_第1页
第1页 / 共36页
CC++:从基础语法到优化策略 (3).pdf_第2页
第2页 / 共36页
点击查看更多>>
资源描述

《CC++:从基础语法到优化策略 (3).pdf》由会员分享,可在线阅读,更多相关《CC++:从基础语法到优化策略 (3).pdf(36页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、C/C+Program DesignCS205Week 3Some operatorsThe slides are based on the book Increment/Decrement Operators Prefixing versus postfixing:+x,x+,-x,x-Prefix form is more efficient The increment/decrement operators and pointersAdding an increment operator to a pointer increases its value by the number of

2、bytes in the type it points toThe prefix increment,prefix decrement,and dereferencing operators have the same precedence(from right to left)Postfix increment and decrement operators have the same precedence,which is higher than the prefix precedence(from left to right)See program example plus_one.cp

3、p The increment(+)and decrement(-)operatorsCombination assignment operators Combination assignment operators See program example combination.cppThe?:Operator Conditional operator(question mark)See program example conditional.cppfor loopThe slides are based on the book for Loops Why needs loop operat

4、ions?Perform repetitive tasks Most tasks have the same process See program example forloop.cpp Increment operator:+operator(i=i+1;)Introducing for Loops Parts of a for Loop Setting a value initially Testing whether the loop should continue Executing the loop actions-body Updating value(s)used for th

5、e testfor(initialization;test-expression;update-expression)body;Introducing for Loops See program example num_test.cpp Decrement operator:-operator(i=i-1;)The range-based for loop(C+11)See Program example cxx11loop.cppColon symbol:&symbol:reference variableTo modify the array contentsMore Examples S

6、ee program example formore.cpp Factorial definitionZero factorial,written as 0!,is defined to be 1(exclamation marks!)The factorial of each integer being the product of that integer with the preceding factorial See program example bigstep.cpp Changing the step sizeExample:Nested Loops(嵌套)and Two-Dim

7、ensional Arrays Example:int maxtemps45;See program example nested.cppRelational ExpressionsThe slides are based on the book Expressions A C+expression is a value or a combination of values and operators Every C+expression has a value A for control section uses three expressions Relational expression

8、s such as x y evaluate to the bool values Evaluating the expression is the primary effectprimary effectEvaluating x+15 calculates a new value,but it doesnt change the value of xBut evaluating+x+15 does have a side effect because it involves incrementing xRelational Expressions C+provides six relatio

9、nal operators to compare numbers Exclamation markComparisons in Test Expression Program example equal.cpp A mistake youll probably make=or=Program example compstr1.cpp Comparing C-style strings strcmp(str1,str2)Program example compstr2.cpp Comparing string class strings Using relational symbol(!=)wh

10、ile loopThe slides are based on the book The while Loop while is entry-condition loop It has just a test condition and a body Do something to affect the test-condition expression See Program example while.cpp Two types of condition expressionwhile(namei!=0)while(namei)for Versus while In C+the for a

11、nd while loops are essentially equivalentMore Loops The do while Loop Its an exit-condition loop Such a loop always executes at least once See Program example dowhile.cppExample:Loops and Text Input Using unadorned cin for input When to stop?A sentinel character See program example textin1.cppThe pr

12、ogram omit the spacesProgram and operating system both work cin.get(char)to the rescue See program example textin2.cppRead the spaceDeclare the argument as a referenceBranching StatementsThe slides are based on the book The if Statement One of the keys to designing intelligent programs is to give th

13、em the ability to make decisions Looping if statement See program example if.cppMore than one selections The if else Statement Decide which of two statements or blocks is executed Must use braces to collect statements into a single block Remember the conditional compilation#if,#else The if else if e

14、lse Construction See program example ifelseif.cppLogical ExpressionsThe slides are based on the book The Logical OR Operator:|Three operatorsLogical OR,written|Logical AND,written&Logical NOT,written!The logical OR operator:|has a lower precedence than the relational operatorsThe|operator is a seque

15、nce pointC+wont bother evaluating the expression on the right if the expression on the left is true See program example or.cppint a=1,b=1;if(a|b+)AND Operator:&NOT Operator:!AND OperatorLower precedence than the relationaloperatorsActs as a sequence pointC+doesnt bother evaluating the right side in

16、some cases NOT Operator Exclamation pointIf expression is true,or nonzero,then!expression is falseIf expression is false,then!expression is trueLogical Operator Facts PrecedenceThe NOT(!)operator has a higher precedence than any of the relationalor arithmetic operatorsThe AND operator has a higher p

17、recedence than the OR operatorUse parentheses to tell the program the interpretation you want The cctype library of character functions A handy package of character-related functions isalnum()isdigit()isspace()NOT-relational-AND-ORswitch StatementThe slides are based on the book The switch Statement

18、 Acts as a routing device that tells the computer which line of code to execute next You must use the break See program example switch.cppMore About switch switch and if elseLet a program select from a list of alternativesA switch statement isnt designed to handle rangesEach switch case label must b

19、e a single valueAlso that value must be an integerA switch statement cant handle floating-point testsbreak and continueStatementsThe slides are based on the book The break and continue Statements The break and continue statements enable a program to skip over parts of the code breakcauses program ex

20、ecution to pass to the next statement following the switch or the loop continuestatement is used in loops and causes a program to skip the rest of the body of the loop and then start a new loop cycle See program example jump.cppExample:Number-Reading Loops What happens if the user responds by enteri

21、ng a wordinstead of a number?See program example cinfish.cpp The preceding example doesnt attempt to read any input after non-numeric inputFile input&outputThe slides are based on the book Simple File Output Main steps for using file output Include the fstream header file Create an ofstream object A

22、ssociate the ofstream object with a file(C-style)using open()Use the ofstream object in the same manner you would use cout Use the close()method to close the file See program example outfile.cppSimple File Input Main steps for using file input Include the fstream header file and account for the std

23、Declare one or more ifstream variables,or objects Associate a ifstream object with a file using open()Use the close()method to close the file Use operator,get(),getline(),method See program example sumafile.cpp What happens if you attempt to open a non-existent file for input?exit(EXIT_FAILURE);Communicate with the operating system Terminate the program

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁