Slide 1Chapter 3OverviewFlow Of Control3.1Using Boolean ExpressionsEvaluating Boolean ExpressionsDisplay 3.1Order of PrecedencePrecedence RulesDisplay 3.2Precedence Rule ExampleEvaluating x + 1 > 2 | | x + 1 < - 3Short-Circuit EvaluationUsing Short-Circuit EvaluationType bool and Type intProblems with !Correcting the ! ProblemAvoiding !bool Return ValuesFunction appropriateEnumeration Types (Optional)Default enum ValuesEnumeration ValuesSection 3.1 Conclusion3.2Multiway BranchesNested StatementsDisplay 3.3Nested if-else StatementsFirst Try Nested if's : Dangling Else ProblemBraces and Nested StatementsDisplay 3.4Multi-way if-else-statementsNumber GuessingIndenting Nested if-elseThe Final if-else-statementNested if-else SyntaxProgram Example: State Income TaxDisplay 3.5 (1/2)Display 3.5 (2/2)Refining if-else-statementsThe switch-statementDisplay 3.6 (1/2)Display 3.6 (2/2)switch-statement SyntaxThe Controlling StatementThe break StatementThe default StatementSwitch-statements and MenusDisplay 3.7 (1/2)Display 3.7 (2/2)Function Calls in BranchesBlocksDisplay 3.8 (1/2)Display 3.8 (2/2)Statement BlocksScope Rule for Nested BlocksSection 3.2 Conclusion3.3More About C++ Loop Statementswhile and do-whileDisplay 3.9The Increment Operatornumber++ vs ++number++ ComparisonsDisplay 3.10The Decrement OperatorThe for-Statementfor/while Loop ComparisonFor Loop Dissectionfor Loop AlternativeDisplay 3.11Display 3.12for-loop DetailsThe for-loop BodyDisplay 3.13The Empty StatementExtra SemicolonLocal Variable StandardWhich Loop To Use?Choosing a for-loopChoosing a while-loopChoosing a do-while LoopThe break-StatementDisplay 3.14Section 3.3 Conclusion3.4Designing LoopsSums and Productsfor-loop for a sumRepeat "this many times"for-loop For a ProductEnding a LoopList Headed By SizeAsk Before IteratingList Ended With a Sentinel ValueRunning Out of InputGeneral Methods To Control LoopsCount Controlled LoopsExit on Flag ConditionExit on Flag CautionThe ProblemThe Exit On Flag SolutionNested LoopsDisplay 3.15Debugging LoopsFixing Off By One ErrorsFixing Infinite LoopsMore Loop Debugging TipsDebugging ExampleTracing VariablesFirst FixSecond FixLoop Testing GuidelinesStarting OverChapter 3.4 ConclusionChapter 3 -- EndCopyright © 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 ControlFlow of control refers to the order in which program statements are performedWe have seen the following ways to specify flow of controlif-else-statementswhile-statementsdo-while-statementsNew methods described in this chapter includeswitch-statementsfor-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 ExpressionsA Boolean Expression is an expression that is either true or falseBoolean 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 valueType bool allows declaration of variables thatcarry the value true or falseSlide 3- 7Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBoolean expressions are evaluated using valuesfrom the Truth Tables inFor 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-WesleyBackNextDisplay 3.1Slide 3- 9Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyOrder of PrecedenceIf parenthesis are omitted from boolean expressions, the default precedence of operations is:Perform ! operations firstPerform relational operations such as < nextPerform && operations nextPerform | | operations lastSlide 3- 10Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyDisplay 3.2Precedence RulesItems in expressions are grouped by precedencerules for arithmetic and boolean operatorsOperators with higher precedence are performed firstBinary operators with equal precedence are performed left to rightUnary operators of equal precedence are performed right to leftSlide 3- 11Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBackNextDisplay 3.2Slide 3- 12Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyPrecedence Rule ExampleThe 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 < - 3Slide 3- 13Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyEvaluating x + 1 > 2 | | x + 1 < - 3Using the precedence rules of Display 3.2First apply the unary – Next apply the +'s Now apply the > and <Finally do the | |Slide 3- 14Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyShort-Circuit EvaluationSome boolean expressions do not need to becompletely evaluatedif x is negative, the value of the expression (x >= 0) && ( y > 1)can be determined by evaluating only (x >= 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 evaluatedSlide 3- 15Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyUsing Short-Circuit EvaluationShort-circuit evaluation can be used to preventrun time errorsConsider this if-statement if ((kids != 0) && (pieces / kids >= 2) ) cout << "Each child may have two pieces!";If the value of kids is zero, short-circuit evaluationprevents evaluation of (pieces / 0 >= 2) Division by zero causes a run-time errorSlide 3- 16Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyType bool and Type intC++ can use integers as if they were Boolean valuesAny
View Full Document