chapter 04 c++

Upload: aljawad14

Post on 02-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Chapter 04 c++

    1/42

    INTRODUCTION TOC++

    Chapter 04: Making Decision

    1

  • 8/10/2019 Chapter 04 c++

    2/42

    Topics

    4.1 Relational Operators

    4.2 The ifStatement

    4.3 The if/elseStatement

    4.4 The if/elseifStatement

    4.5 Menu-Driven Programs

    4.6 Logical Operators4.7 Validating User Input4.8 More about Variable Definitions and Scope4.9 Comparing Characters and Strings4.10 The Conditional Operator4.11 The switchStatement

    4-2

  • 8/10/2019 Chapter 04 c++

    3/42

    4.1 Relational Operators

    Used to compare numbers to determine

    relative order

    Operators:

    4-3

    > Greater than

    < Less than

    >= Greater than or equal to

  • 8/10/2019 Chapter 04 c++

    4/42

    Relational Expressions

    Relational expressions are Boolean (i.e.,evaluate to trueor false)

    Examples:12 > 5is true

    7

  • 8/10/2019 Chapter 04 c++

    5/42

    Relational Expressions

    Can be assigned to a variable

    boolresult = (x

  • 8/10/2019 Chapter 04 c++

    6/42

    4.2 The ifStatement

    Allows statements to be conditionally

    executed or skipped over

    Models the way we mentally evaluate

    situations

    If it is cold outside,

    wear a coat and wear a hat.

    4-6

  • 8/10/2019 Chapter 04 c++

    7/42

    Format of the ifStatement

    if (condition){

    statement1;statement2;

    statementn;

    }The block inside the braces is called the bodyof the ifstatement. If there is only 1 statementin the body, the {}may be omitted.

    4-7

    No ;

    goes here

    ;goes here

  • 8/10/2019 Chapter 04 c++

    8/42

    How the ifStatement Works

    If (condition)is true, then thestatement(s)in the body areexecuted.

    If (condition)is false, then thestatement(s)are skipped.

    4-8

  • 8/10/2019 Chapter 04 c++

    9/42

    ifStatement Flow of Control

    4-9

    condition

    1 or more

    statements

    true false

  • 8/10/2019 Chapter 04 c++

    10/42

    Example ifStatements

    if (score >= 60)

    cout = 90)

    {

    grade = 'A';

    cout

  • 8/10/2019 Chapter 04 c++

    11/42

    ifStatement Notes

    Do not place ;after (condition)

    Don't forget the { } around a multi-statement

    body

    Place each statement;on a separate lineafter (condition), indented

    0 is false;any other value is true

    4-11

  • 8/10/2019 Chapter 04 c++

    12/42

    What is trueand false?

    An expression whose value is 0 is consideredfalse.

    An expression whose value is non-zero isconsidered true.

    An expression need not be a comparisonit

    can be a single variable or a mathematical

    expression.

    4-12

  • 8/10/2019 Chapter 04 c++

    13/42

    Flag

    A variable that signals a condition

    Usually implemented as abool

    Meaning:

    true: the condition exists

    false: the condition does not exist

    The flag value can be both set and tested withifstatements

    4-13

  • 8/10/2019 Chapter 04 c++

    14/42

    Flag Example

    Example:bool validMonths = true;

    if (months < 0)validMonths = false;

    if (validMonths)

    moPayment = total / months;

    4-14

  • 8/10/2019 Chapter 04 c++

    15/42

    Comparisons with floating-point

    numbers

    It is difficult to test for equality when working

    with floating point numbers.

    It is better to use greater than, less than tests, or test to see if value is very close to a given value

    4-15

  • 8/10/2019 Chapter 04 c++

    16/42

    4.3 The if/elseStatement

    Allows a choice between statementsdepending on whether (condition)istrueor false

    Format: if (condition){

    statement set 1;}

    else{statement set 2;

    }

    4-16

  • 8/10/2019 Chapter 04 c++

    17/42

    How the if/elseWorks

    If (condition)is true, statementset1is executed and statementset2is skipped.

    If (condition)is false, statementset1is skipped and statementset2is executed.

    4-17

  • 8/10/2019 Chapter 04 c++

    18/42

    if/elseFlow of Control

    4-18

    condition

    statement

    set 1

    true false

    statement

    set 2

  • 8/10/2019 Chapter 04 c++

    19/42

    Example if/elseStatements

    if (score >= 60)cout

  • 8/10/2019 Chapter 04 c++

    20/42

    4.4 The if/elseifStatement

    Chain of ifstatements that test in orderuntil one is found to be true

    Also models thought processes

    If it is raining, take an umbrella,

    else, if it is windy, take a hat,

    else, if it is sunny, take sunglasses.

    4-20

  • 8/10/2019 Chapter 04 c++

    21/42

    if/elseifFormat

    if (condition 1)

    { statement set 1;

    }

    else if (condition 2)

    { statement set 2;

    }

    else if (condition n)

    { statement set n;

    }

    4-21

  • 8/10/2019 Chapter 04 c++

    22/42

    Using a Trailing else

    Used with if/elseifstatement when all ofthe conditions are false

    Provides a default statement or action Can be used to catch invalid values or handle

    other exceptional situations

    4-22

  • 8/10/2019 Chapter 04 c++

    23/42

  • 8/10/2019 Chapter 04 c++

    24/42

    4.5 Menu-Driven Program

    Menu: list of choices presented to the user on

    the computer screen

    Menu-driven program: program executioncontrolled by user selecting from a list of

    actions

    Menu can be implemented using if/elseif

    statements

    4-24

  • 8/10/2019 Chapter 04 c++

    25/42

    Menu-driven Program

    Organization

    Display list of numbered or lettered choices for

    actions.

    Input users selection of number or letter

    Test user selection in (condition)

    if a match, then execute code to carry out desired

    action if not, then test with next (condition)

    4-25

  • 8/10/2019 Chapter 04 c++

    26/42

    4.6 Logical Operators

    Used to create relational expressions fromother relational expressions

    Operators, Meaning, and Explanation

    4-26

    && AND New relational expression is true ifboth expressions are true

    || OR New relational expression is true if

    either expression is true

    ! NOTReverses the value of an expression;true expression becomes false, falseexpression becomes true

  • 8/10/2019 Chapter 04 c++

    27/42

    Logical Operator Examples

    int x = 12, y = 5, z = -4;

    (x > y) && (y > z) true

    (x > y) && (z > y) false

    (x

  • 8/10/2019 Chapter 04 c++

    28/42

    Logical Precedence

    Highest !&&

    Lowest ||

    Example:(2 < 3) || (5 > 6) && (7 > 8)

    is true because AND is evaluated before OR

    4-28

  • 8/10/2019 Chapter 04 c++

    29/42

  • 8/10/2019 Chapter 04 c++

    30/42

  • 8/10/2019 Chapter 04 c++

    31/42

    4.7 Validating User Input

    Input validation: inspecting input data to

    determine if it is acceptable

    Want to avoid accepting bad input

    Can perform various tests

    Range

    Reasonableness

    Valid menu choice Zero as a divisor

    4-31

    4 8 M Ab t V i bl D fi iti

  • 8/10/2019 Chapter 04 c++

    32/42

    4.8 More About Variable Definitions

    and Scope

    Scopeof a variable is the block in which it is

    defined, from the point of definition to the end of

    the block

    Variables are usually defined at beginning of

    function

    They may instead be defined close to first use

    4-32

    M Ab t V i bl D fi iti

  • 8/10/2019 Chapter 04 c++

    33/42

    More About Variable Definitions

    and Scope

    Variables defined inside {}have localorblock scope

    When in a block that is nested inside anotherblock, you can define variables with the samename as in the outer block.

    When the program is executing in the innerblock, the outer definition is not available

    This is generally not a good idea

    4-33

    4 9 Comparing Characters and

  • 8/10/2019 Chapter 04 c++

    34/42

    4.9 Comparing Characters and

    Strings

    Can use relational operators with characters andstring objects

    if (menuChoice == 'A')

    if (firstName == "Beth")

    Comparing characters is really comparing ASCIIvalues of characters

    Comparing string objects is comparing the ASCII

    values of the characters in the strings. Comparisonis character-by-character

    Cannot compare C-style strings with relationaloperators

    4-34

  • 8/10/2019 Chapter 04 c++

    35/42

    4.10 The Conditional Operator

    Can use to create short if/elsestatements

    Format: expr ? expr : expr;

    4-35

  • 8/10/2019 Chapter 04 c++

    36/42

    4.11 The switchStatement

    Used to select among statements from severalalternatives

    May sometimes be used instead of if/elseifstatements

    4-36

  • 8/10/2019 Chapter 04 c++

    37/42

    it h Statement

  • 8/10/2019 Chapter 04 c++

    38/42

    switchStatement

    Requirements

    1) IntExpressionmust be a charor an

    integer variable or an expression that

    evaluates to an integer value

    2) exp1through expnmust be constant

    integer type expressions and must be uniquein the switchstatement

    3) defaultis optional but recommended

    4-38

  • 8/10/2019 Chapter 04 c++

    39/42

    How the switchStatement Works

    1) IntExpressionis evaluated

    2) The value of intExpressionis compared

    against exp1through expn.3) If IntExpressionmatches value expi, the

    program branches to the statement(s)following expiand continues to the end of the

    switch4) If no matching value is found, the program

    branches to the statement after default:

    4-39

  • 8/10/2019 Chapter 04 c++

    40/42

    ThebreakStatement

    Used to stop execution in the current block

    Also used to exit a switchstatement

    Useful to execute a single casestatementwithout executing statements following it

    4-40

  • 8/10/2019 Chapter 04 c++

    41/42

    Example switchStatement

    switch (gender) {

    case 'f': cout

  • 8/10/2019 Chapter 04 c++

    42/42

    Using switchwith a Menu

    switchstatement is a natural choice for

    menu-driven program

    display menu

    get user input use user input as IntExpressionin switch

    statement

    use menu choices as expto test against in the

    casestatements