chapter 3 - control structures - selection

Upload: fakensta

Post on 14-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    1/34

    Copyright 2012 Pearson Education, Inc.

    Chapter 3

    Control Structures:Selection

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    2/34

    Copyright 2012 Pearson Education, Inc.

    Outline

    Objectives1. Algorithm Development

    2. Structured Programming

    3. Conditional Expressions

    4. Selection Statements: if Statement

    5. Numerical Technique: Linear Interpolation

    6. Problem Solving Applied: Freezing Temperature of

    Seawater

    7. Selection Statements: switchStatement

    8. Defining Operators for Programmer-defined data types

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    3/34

    Copyright 2012 Pearson Education, Inc.

    Objectives

    Develop problem-solving solutions inC++ containing:

    Conditional expressions that evaluate to

    true of false.

    Selection structures that allow us to

    provide alternative paths in a program.

    Algorithm development and descriptions of

    algorithms using flowcharts and

    pseudocode.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    4/34

    Algorithm Development

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    5/34

    Algorithm Development

    An algorithm is a sequence of steps for

    solving a problem.

    Engineering problem solutions to real

    world problems require complex

    algorithms.

    Development of a good algorithm

    increases the quality and maintainability of

    a solution, and reduces the overall time

    required to implement a correct solution.Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    6/34

    Top-Down Design

    Top-down design begins with a "big

    picture" description of a problem solution

    in sequential steps.

    The sequential steps are refined until the

    steps are detailed enough to translate to

    language statements.

    The refined steps, or algorithm, can be

    described using pseudo code or

    flowcharts.Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    7/34

    Copyright 2012 Pearson Education, Inc.

    PseudocodeNotation

    and

    Flowchart

    Symbols

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    8/34

    Structured Programming

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    9/34

    Structured Programming

    A structured program is written using

    simple control structures, including:

    Sequence steps are performed one after

    another.

    Selection one set of statements is executed

    if a given condition is true, a different set of

    statements, or no statements at all, is executedif the condition is false.

    Repetition A set of statements is executed

    repeatedly as long as a given condition is true.Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    10/34

    Structured Programming

    Sequence

    Selection

    Repetition

    Copyright 2012 Pearson Education, Inc.

    ?truefalse

    ?true

    false

    ? => conditional

    expression

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    11/34

    Conditional Expressions

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    12/34

    Conditional Expressions

    A conditional expression is a

    Boolean expression that evaluates to true

    or false.

    Selection structures and repetition

    structures rely on conditional expressions.

    Relationaloperators and logicaloperators are used to form conditional

    expressions.

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    13/34

    Relational Operators

    == equality

    != non equality

    < less than

    > greater than

    = greater than equal to

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    14/34

    Logical Operators

    ! not

    && and

    || or

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    15/34

    Logical Operators

    Truth table for conditional expressions

    Copyright 2012 Pearson Education, Inc.

    A B A&&B A||B !A !B

    false false false false true true

    false true false true true false

    true false false true false true

    true true true true false false

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    16/34

    Short Circuiting

    C++ only evaluates as much of an

    expression as necessary to evaluate it.

    E.g. if A is false, A && B is always false,

    regardless of the value of B.

    E.g. if A is true, A || B is always true,

    regardless of the value of B.

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    17/34

    Operator Precedence

    Copyright 2012 Pearson Education, Inc.

    Precedence Operator Associativity

    1 () Innermost First

    2 Unary operators: + - ++ -- ! (type) Right to left

    3 * / % Left to right

    4 + - Left to right

    5 < => Left to right

    6 == != Left to right

    7 && Left to right

    8 || Left to right

    9 = += -= *= /= %= Right to left

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    18/34

    Selection Statements:

    if Statement

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    19/34

    The if Statement

    Copyright 2012 Pearson Education, Inc.

    Simplified Syntax

    if (boolean_expression)

    statement;

    if (boolean_expression) {

    statement1;

    statement_n;

    }

    Examples

    if (x>0)++k; //statement executed iff x>0

    if (x>0) { //statement block executed iff x>0x = sqrt(x);++k;

    }

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    20/34

    The if-else Statement

    Copyright 2012 Pearson Education, Inc.

    Syntax

    if (boolean_expression)

    statement;

    [else statement;]

    if (boolean_expression) {

    } [else {

    } ]

    Example

    if (x>0) { //statement block executed iff x>0

    } else { //statement block executed iff x

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    21/34

    Avoiding Bugs

    To avoid confusion and possible

    errors when using if/else statements, you

    should use {} to clearly define statement

    blocks.

    Do not use == with real values

    Instead ofx==3.14, use

    fabs(x-3.14)

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    22/34

    Numerical Technique:

    Linear Interpolation

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    23/34

    Why Linear Interpolation?

    Collecting data in a computer usually

    results in a collection of samples.

    Often, we are interested in what happens

    in between the sample points.

    i.e. we need to interpolate between the

    samples.

    Linear and cubic spline interpolationcommon techniques.

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    24/34

    Interpolation Example

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    25/34

    Linear Interpolation

    Copyright 2012 Pearson Education, Inc.

    f(a), a, f(c), and c

    are known. We

    wish to estimate

    f(b) where

    a < b < c.

    Linear

    interpolation is astraight-line

    approximation

    between the

    known points.

    P bl S l i A li d

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    26/34

    Problem Solving Applied:

    Freezing Temperature

    of Seawater

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    27/34

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    28/34

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    29/34

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    30/34

    Selection Statements:

    switch Statement

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    31/34

    The switch Statement

    Copyright 2012 Pearson Education, Inc.

    Syntax

    switch (control_expression) {

    case constant:

    statement(s);break;

    [ case constant:

    statement(s);

    break;

    [] ]

    [ default: statement(s); ]

    }

    Control expression must

    be an integral type (e.g.

    char, int, etc) Break statements are not

    required; without a break

    statement execution runs

    through other case labels.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    32/34

    Examplecharch;

    int ecount=0, vowels=0, other=0;

    cin.get(ch);

    while(!cin.eof()) {

    switch(ch) {casee: ecount++;

    casea:casei:

    caseo:

    caseu: vowels++;

    break;

    default: other++;

    } //end switchcin.get(ch);

    } //end while

    cout

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    33/34

    Defining Operators for

    Programmer-defined

    data types

    Copyright 2012 Pearson Education, Inc.

  • 7/29/2019 Chapter 3 - Control Structures - Selection

    34/34

    Operators and Programmer-

    defined Types Only the assignment operation is

    defined for programmer-defined types.

    The programmer may over loadoperators

    by providing a new definition of an existing

    operator to work with programmer-defined

    types.

    Operators may not be overloaded forstandard C++ datatypes.

    Copyright 2012 Pearson Education Inc