Unformatted text preview:

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 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-WesleyBackNextDisplay 3.1Slide 3- 9Copyright © 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- 10Copyright © 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- 11Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyBackNextDisplay 3.2Slide 3- 12Copyright © 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 < - 3Slide 3- 13Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyEvaluating x + 1 > 2 | | x + 1 < - 3Using the precedence rules of Display 3.2First 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 EvaluationSome boolean expressions do not need to becompletely evaluatedif 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 EvaluationShort-circuit evaluation can be used to preventrun time errorsConsider 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 intC++ can use integers as if they were Boolean valuesAny


View Full Document

GSU CSC 2311 - Savitch_ch_03

Documents in this Course
ch14

ch14

65 pages

ch13

ch13

68 pages

ch06

ch06

110 pages

ch10

ch10

80 pages

Load more
Download Savitch_ch_03
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Savitch_ch_03 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Savitch_ch_03 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?