C++ Basics - More Flow of Control

118 350 0

Meo Gửi tin nhắn Báo tài liệu vi phạm

Tải lên: 1,274 tài liệu

  • Loading ...
1/118 trang
Tải xuống

Thông tin tài liệu

Ngày đăng: 12/09/2012, 22:47

C++ Basics - More Flow of Control Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyChapter 3More Flow of ControlSlide 3- 3Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyOverview3.1 Using Boolean Expressions 3.2 Multiway Branches3.3 More about C++ Loop Statements3.4 Designing LoopsSlide 3- 4Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyFlow Of ControlFlow of control refers to the order in which program statements are performedWe have seen the following ways to specify flow of controlif-else-statementswhile-statementsdo-while-statementsNew methods described in this chapter includeswitch-statementsfor-statementsCopyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley3.1Using Boolean ExpressionsSlide 3- 6Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyUsing Boolean ExpressionsA Boolean Expression is an expression that is either true or falseBoolean expressions are evaluated using relational operations such as= = , < , and >= which produce a boolean value and boolean operations such as&&, | |, and ! which also produce a boolean valueType bool allows declaration of variables thatcarry the value true or falseSlide 3- 7Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBoolean expressions are evaluated using valuesfrom the Truth Tables inFor example, if y is 8, the expression !( ( y < 3) | | ( y > 7) ) is evaluated in the following sequenceDisplay 3.1! ( false | | true )! ( true )falseEvaluating Boolean ExpressionsSlide 3- 8Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyOrder of PrecedenceIf parenthesis are omitted from boolean expressions, the default precedence of operations is:Perform ! operations firstPerform relational operations such as < nextPerform && operations nextPerform | | operations lastSlide 3- 9Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyDisplay 3.2Precedence RulesItems in expressions are grouped by precedencerules for arithmetic and boolean operatorsOperators with higher precedence are performed firstBinary operators with equal precedence are performed left to rightUnary operators of equal precedence are performed right to leftSlide 3- 10Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyPrecedence Rule ExampleThe expression (x+1) > 2 | | (x + 1) < -3is equivalent to ( (x + 1) > 2) | | ( ( x + 1) < -3)Because > and < have higher precedence than | | and is also equivalent to x + 1 > 2 | | x + 1 < - 3[...]... 0) C++ uses short-circuit evaluation If the value of the leftmost sub-expression determines the final value of the expression, the rest of the expression is not evaluated Slide 3- 49Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About C++ Loop StatementsA loop is a program construction that repeats a statement or sequence of statements a number of timesThe... Pearson Addison-WesleyDisplay 3.7 (1)Display 3.7 (2)Switch-statements and MenusNested if-else statements are more versatile thana switch statementSwitch-statements can make some code more clearA menu is a natural application for a switch-statement Slide 3- 11Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyEvaluating x + 1 > 2 | | x + 1 < - 3Using... pieces!";If the value of kids is zero, short-circuit evaluationprevents evaluation of (pieces / 0 >= 2) Division by zero causes a run-time error Slide 3- 12Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyShort-Circuit EvaluationSome boolean expressions do not need to becompletely evaluatedif x is negative, the value of the expression (x >= 0)... operators of equal precedence are performed right to left Slide 3- 44Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyDisplay 3.8 (1)Display 3.8 (2)BlocksEach branch of a switch or if-else statement isa separate sub-taskIf the action of a branch is too simple to warrant a function call, use multiple statements between bracesA block is a section of code enclosed... Pearson Addison-WesleyCorrecting the ! ProblemThe intent of the previous expression was most likely the expression ( ! ( time > limit) )which evaluates as ( ! ( false) ) true Slide 3- 13Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyUsing Short-Circuit EvaluationShort-circuit evaluation can be used to preventrun time errorsConsider this if-statement...Slide 3- 22Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyEnumeration ValuesUnless specified, the value assigned an enumeration constant is 1 more than the previousconstantEnum MyEnum{ONE = 17, TWO, THREE, FOUR = -3 , FIVE};results in these valuesONE = 17, TWO = 18, THREE = 19, FOUR = -3 , FIVE = -2 Slide 3- 42Copyright © 2007 Pearson... demonstrates the use of braces in nested if-else-statementsDisplay 3.4Braces and Nested Statements Slide 3- 17Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyAvoiding !Just as not in English can make things not undifficult to read, the ! operator canmake C++ expressions difficult to understandBefore using the ! operator see if you can express the same idea more clearly... as Pearson Addison-WesleyDefault enum ValuesIf numeric values are not specified, identifiersare assigned consecutive values starting with 0enum Direction { NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3};is equivalent toenum Direction {NORTH, SOUTH, EAST, WEST}; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyChapter 3 More Flow of Control Slide 3- 26Copyright ©... Addison-Wesleyswitch-statement Syntaxswitch (controlling expression){ case Constant_1: statement_Sequence_1break; case Constant_2: Statement_Sequence_2 break; . . . case Constant_n: Statement_Sequence_n break; default: Default_Statement_Sequence} Slide 3- 43Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyFunction Calls in BranchesSwitch and if-else-statements... expression at the beginning of the loopA while loop might never be executed!A do-while loop checks the Boolean expression at the end of the loopA do-while loop is always executed at least onceReview while and do-while syntax in Slide 3- 29Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBraces in nested statements are like parenthesis in arithmetic expressionsBraces . Control Flow of control refers to the order in which program statements are performedWe have seen the following ways to specify flow of control if-else-statementswhile-statementsdo-while-statementsNew. More about C++ Loop Statements3.4 Designing LoopsSlide 3- 4Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyFlow Of Control Flow
- Xem thêm -

Xem thêm: C++ Basics - More Flow of Control, C++ Basics - More Flow of Control, C++ Basics - More Flow of Control

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay