chapter 4 making desisionshvh

Upload: honkytonkz

Post on 04-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Chapter 4 Making Desisionshvh

    1/79

    MAKING DESISIONS

    Introduction to C++

    4-1

  • 7/29/2019 Chapter 4 Making Desisionshvh

    2/79

    Relational Operators4.1

  • 7/29/2019 Chapter 4 Making Desisionshvh

    3/79

    Relational Operators

    Used to compare numbers to determine

    relative order

    Operators:

    4-3

    > Greater than

    < Less than

    >= Greater than or equal to

  • 7/29/2019 Chapter 4 Making Desisionshvh

    4/79

    Relational Expressions

    Boolean expressionstrue or false

    Examples:

    12 > 5 is true

    7

  • 7/29/2019 Chapter 4 Making Desisionshvh

    5/79

    Relational Expressions

    Can be assigned to a variable:

    result = x

  • 7/29/2019 Chapter 4 Making Desisionshvh

    6/79

    The if Statement4.2

  • 7/29/2019 Chapter 4 Making Desisionshvh

    7/79

    The if Statement

    Allows statements to be conditionally

    executed or skipped over

    Models the way we mentally evaluate

    situations:

    "If it is raining, take an umbrella."

    "If it is cold outside, wear a coat."

    4-7

  • 7/29/2019 Chapter 4 Making Desisionshvh

    8/79

    4-8

    Flowchart for Evaluating a Decision

  • 7/29/2019 Chapter 4 Making Desisionshvh

    9/79

    Flowchart for Evaluating a Decision

    4-9

  • 7/29/2019 Chapter 4 Making Desisionshvh

    10/79

    The if Statement

    General Format:

    if (expression)

    statement;

    4-10

  • 7/29/2019 Chapter 4 Making Desisionshvh

    11/79

    if statement what happens

    To evaluate:

    if (expression)

    statement;

    If the expression is true, then

    statement is executed.

    If the expressionis false, then

    statement is skipped.

    4-11

  • 7/29/2019 Chapter 4 Making Desisionshvh

    12/79

    4-12

    (Program Continues)

  • 7/29/2019 Chapter 4 Making Desisionshvh

    13/79

    4-13

  • 7/29/2019 Chapter 4 Making Desisionshvh

    14/79

    Flowchart for Lines 21 and 22

    4-14

  • 7/29/2019 Chapter 4 Making Desisionshvh

    15/79

    if statement notes

    Do not place ; after (expression)

    Place statement; on a separate line after(expression), indented:

    if (score > 90)

    grade = 'A';

    Be careful testing floats and doubles

    for equality 0 is false; any other value is true

    4-15

  • 7/29/2019 Chapter 4 Making Desisionshvh

    16/79

    Flags4.3

  • 7/29/2019 Chapter 4 Making Desisionshvh

    17/79

    Flags

    Variable that signals a condition

    Usually implemented as a bool variable

    As with other variables in functions, must beassigned an initial value before it is used

    4-17

  • 7/29/2019 Chapter 4 Making Desisionshvh

    18/79

    Expanding the if Statement4.4

  • 7/29/2019 Chapter 4 Making Desisionshvh

    19/79

    Expanding the if Statement

    To execute more than one statement as part of an if

    statement, enclose them in { }:

    if (score > 90)

    {grade = 'A';

    cout

  • 7/29/2019 Chapter 4 Making Desisionshvh

    20/79

    The if/else Statement4.5

  • 7/29/2019 Chapter 4 Making Desisionshvh

    21/79

    The if/else Statement

    Provides two possible paths of execution

    Performs one statement or block if the

    expression is true, otherwise performsanother statement or block.

    4-21

  • 7/29/2019 Chapter 4 Making Desisionshvh

    22/79

    The if/else Statement

    General Format:

    if (expression)

    statement1; // or block

    else

    statement2; // or block

    4-22

  • 7/29/2019 Chapter 4 Making Desisionshvh

    23/79

    if/else what happens

    To evaluate:if (expression)

    statement1;

    else

    statement2;

    If the expression is true, then statement1 is executedand statement2 is skipped.

    If the expression is false, then statement1 is skipped

    and statement2 is executed.

    4-23

  • 7/29/2019 Chapter 4 Making Desisionshvh

    24/79

    4-24

  • 7/29/2019 Chapter 4 Making Desisionshvh

    25/79

    Flowchart for Lines 14 through 18

    4-25

  • 7/29/2019 Chapter 4 Making Desisionshvh

    26/79

    4-26

    (Program Continues)

  • 7/29/2019 Chapter 4 Making Desisionshvh

    27/79

    4-27

  • 7/29/2019 Chapter 4 Making Desisionshvh

    28/79

    Nested if Statements4.6

  • 7/29/2019 Chapter 4 Making Desisionshvh

    29/79

    Nested ifStatements

    An if statement that is nested inside another

    if statement

    Nested if statements can be used to test

    more than one condition

    4-29

  • 7/29/2019 Chapter 4 Making Desisionshvh

    30/79

    Flowchart for a Nested if Statement

    4-30

  • 7/29/2019 Chapter 4 Making Desisionshvh

    31/79

    Nested if Statements

    From Program 4-10

    4-31

  • 7/29/2019 Chapter 4 Making Desisionshvh

    32/79

    Nested if Statements

    Another example, from Program 4-11

    4-32

  • 7/29/2019 Chapter 4 Making Desisionshvh

    33/79

    Use Proper Indentation!

    4-33

  • 7/29/2019 Chapter 4 Making Desisionshvh

    34/79

    The if/else if

    Statement

    4.7

  • 7/29/2019 Chapter 4 Making Desisionshvh

    35/79

    The if/else ifStatement

    Tests a series of conditions until one is found to betrue

    Often simpler than using nested if/elsestatements

    Can be used to model thought processes such as:

    "If it is raining, take an umbrella,else, if it is windy, take a hat,else, take sunglasses

    4-35

  • 7/29/2019 Chapter 4 Making Desisionshvh

    36/79

    if/else if format

    if (expression)

    statement1; // or block

    else if (expression)

    statement2; // or block

    .

    . // other else ifs

    .

    else if (expression)

    statementn; // or block

    4-36

  • 7/29/2019 Chapter 4 Making Desisionshvh

    37/79

    From Program 4-13

    4-37

  • 7/29/2019 Chapter 4 Making Desisionshvh

    38/79

    Using a Trailing else to Catch Errors

    The trailing else clause is optional, but is bestused to catch errors

    4-38

    15 // Determine the letter grade.16 if (testScore < 60)

    17 cout

  • 7/29/2019 Chapter 4 Making Desisionshvh

    39/79

    Menus4.8

  • 7/29/2019 Chapter 4 Making Desisionshvh

    40/79

    Menus

    Menu-driven program: program execution

    controlled by user selecting from a list of

    actions Menu: list of choices on the screen

    Menus can be implemented using if/else

    if statements

    4-40

  • 7/29/2019 Chapter 4 Making Desisionshvh

    41/79

    Menu-driven program organization

    Display list of numbered or lettered choices for

    actions

    Prompt user to make selection

    Test user selection in expression

    if a match, then execute code for action

    if not, then go on to next expression

    4-41

  • 7/29/2019 Chapter 4 Making Desisionshvh

    42/79

    Logical Operators4.9

  • 7/29/2019 Chapter 4 Making Desisionshvh

    43/79

    Logical Operators

    Used to create relational expressions from

    other relational expressions

    Operators, meaning, and explanation:

    4-43

    && AND New relational expression is true if bothexpressions are true

    || OR New relational expression is true if eitherexpression is true

    ! NOT Reverses the value of an expression trueexpression becomes false, and false becomestrue

  • 7/29/2019 Chapter 4 Making Desisionshvh

    44/79

    Logical Operators - examples

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

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

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

    (x

  • 7/29/2019 Chapter 4 Making Desisionshvh

    45/79

    The && Operator in Program 4-16

    4-45

  • 7/29/2019 Chapter 4 Making Desisionshvh

    46/79

    The || Operator in Program 4-17

    4-46

  • 7/29/2019 Chapter 4 Making Desisionshvh

    47/79

    The ! Operator in Program 4-18

    4-47

  • 7/29/2019 Chapter 4 Making Desisionshvh

    48/79

    Logical Operators - notes

    ! has highest precedence, followed by &&,

    then ||

    If the value of an expression can be

    determined by evaluating just the sub-

    expression on left side of a logical operator,

    then the sub-expression on the right side will

    not be evaluated (short circuit evaluation)

    4-48

  • 7/29/2019 Chapter 4 Making Desisionshvh

    49/79

    Checking Numeric Ranges

    with Logical Operators

    4.10

    Checking Numeric Ranges with Logical

  • 7/29/2019 Chapter 4 Making Desisionshvh

    50/79

    Checking Numeric Ranges with Logical

    Operators

    Used to test to see if a value falls inside a range:if (grade >= 0 && grade

  • 7/29/2019 Chapter 4 Making Desisionshvh

    51/79

    Validating User Input4.11

  • 7/29/2019 Chapter 4 Making Desisionshvh

    52/79

    Validating User Input

    Input validation: inspecting input data todetermine whether it is acceptable

    Bad output will be produced from bad input

    Can perform various tests: Range

    Reasonableness

    Valid menu choice

    Divide by zero

    4-52

    From Program 4 19

  • 7/29/2019 Chapter 4 Making Desisionshvh

    53/79

    4-53

    From Program 4-19

  • 7/29/2019 Chapter 4 Making Desisionshvh

    54/79

    More About Variable

    Definitions and Scope

    4.12

    M Ab t

  • 7/29/2019 Chapter 4 Making Desisionshvh

    55/79

    More About

    Variable Definitions and Scope

    Scope of a variable is the block in which it is

    defined, from the point of definition to the

    end of the block

    Usually defined at beginning of function

    May be defined close to first use

    4-55

  • 7/29/2019 Chapter 4 Making Desisionshvh

    56/79

    4-56

    From Program 4-21

    Still M Ab t

  • 7/29/2019 Chapter 4 Making Desisionshvh

    57/79

    Still More About

    Variable Definitions and Scope

    Variables defined inside { } have local or

    block scope

    When inside a block within another block,

    can define variables with the same name asin the outer block.

    When in inner block, outer definition is notavailable

    Not a good idea

    4-57

  • 7/29/2019 Chapter 4 Making Desisionshvh

    58/79

    Comparing Strings4.13

  • 7/29/2019 Chapter 4 Making Desisionshvh

    59/79

    Comparing Strings

    You cannot use relational operators with C-

    strings

    Must use the strcmp function to compare C-

    strings

    strcmp compares the ASCII codes of the

    characters in the C-strings. Comparison is

    character-by-character

    4-59

  • 7/29/2019 Chapter 4 Making Desisionshvh

    60/79

    Comparing Strings

    The expression

    strcmp(str1, str2)

    compares thestrings str1 and str2

    It returns 0 if the strings are the same

    It returns a negative number ifstr1 < str2

    It returns a positive number ifstr1 > str2

    4-60

  • 7/29/2019 Chapter 4 Making Desisionshvh

    61/79

    4-61

  • 7/29/2019 Chapter 4 Making Desisionshvh

    62/79

    4-62

  • 7/29/2019 Chapter 4 Making Desisionshvh

    63/79

    The Conditional Operator

    4.14

  • 7/29/2019 Chapter 4 Making Desisionshvh

    64/79

    The Conditional Operator

    Can use to create short if/else statements

    Format: expr ? expr : expr;

    4-64

    x

  • 7/29/2019 Chapter 4 Making Desisionshvh

    65/79

    The Conditional Operator

    The value of a conditional expression is

    The value of the second expression if the first

    expression is true

    The value of the third expression if the firstexpression is false

    Parentheses () may be needed in an

    expression due to precedence of conditionaloperator

    4-65

  • 7/29/2019 Chapter 4 Making Desisionshvh

    66/79

    4-66

  • 7/29/2019 Chapter 4 Making Desisionshvh

    67/79

    The switch Statement

    4.15

  • 7/29/2019 Chapter 4 Making Desisionshvh

    68/79

    The switch Statement

    Used to select among statements from several

    alternatives

    In some cases, can be used instead of

    if/else ifstatements

    4-68

  • 7/29/2019 Chapter 4 Making Desisionshvh

    69/79

    switch statement format

    switch (expression) //integer

    {

    case exp1: statement1;

    case exp2: statement2;

    ...

    case expn: statementn;

    default: statementn+1;}

    4-69

  • 7/29/2019 Chapter 4 Making Desisionshvh

    70/79

    4-70

  • 7/29/2019 Chapter 4 Making Desisionshvh

    71/79

    switch statement requirements

    1) expression must be an integer variable or

    an expression that evaluates to an integer

    value

    2) exp1 through expn must be constant

    integer expressions or literals, and must be

    unique in the switch statement

    3) default is optional but recommended

    4-71

  • 7/29/2019 Chapter 4 Making Desisionshvh

    72/79

    switch statement how it works

    1) expression is evaluated

    2) The value ofexpression is compared againstexp1 through expn.

    3) If expression matches value expi, theprogram branches to the statement followingexpi and continues to the end of the switch

    4) If no matching value is found, the program

    branches to the statement after default:

    4-72

  • 7/29/2019 Chapter 4 Making Desisionshvh

    73/79

    break statement

    Used to exit a switch statement

    If it is left out, the program "falls through" the

    remaining statements in the switchstatement

    4-73

  • 7/29/2019 Chapter 4 Making Desisionshvh

    74/79

    4-74

  • 7/29/2019 Chapter 4 Making Desisionshvh

    75/79

    4-75

    h

  • 7/29/2019 Chapter 4 Making Desisionshvh

    76/79

    Using switch with a menu

    switch statement is a natural choice for

    menu-driven program:

    display the menu

    then, get the user's menu selection

    use user input as expression in switch

    statement

    use menu choices as exprin case statements

    4-76

  • 7/29/2019 Chapter 4 Making Desisionshvh

    77/79

    Testing for File Open Errors4.16

    f

  • 7/29/2019 Chapter 4 Making Desisionshvh

    78/79

    Testing for File Open Errors

    Can test a file stream object to detect if an openoperation failed:

    infile.open("test.txt");

    if (!infile){

    cout

  • 7/29/2019 Chapter 4 Making Desisionshvh

    79/79

    Reading Assignment

    Please Read Chapter 4 of the Gaddis book