BC101 Lecture 4 - Program Structures

Embed Size (px)

Citation preview

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    1/35

    if Statements

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    2/35

    Slide 2

    Introduction

    Sequence

    Statements in the given order

    Condition statement (branching)

    Chooses between two (or more) sequences depending on some conditionif {

    }

    else {

    }

    Iteration statement (looping)repetitively execute a given sequence

    while {

    }

    Three program structures:

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    3/35

    Slide 3

    Structured programming

    Any program can be written as a sequence ofthree basic program structures!!!

    1. sequences,

    2. conditionals,3. and iterations

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    4/35

    Slide 4

    The fundamental if-else Statemen

    Syntax

    if (Expression)

    Action1else

    Action2 If Expression is true then

    execute Action1 otherwise

    execute Action2

    Exampleif(v == 0)

    cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    5/35

    Slide 5

    if

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    6/35

    Slide 6

    Example: Finding the Big One

    int main() {

    int value1;

    int value2;

    int larger;

    cout > value1 >> value2;

    if(value1 > value2)

    larger = value1;

    else

    larger = value2;

    cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    7/35

    Slide 7

    Example: Absolute Value (1st )// program to read number & print its absolute value

    #include

    int main(){

    int value;

    int absvalue;

    cout > value;

    if(value < 0)

    absvalue = -value;

    else absvalue = value;

    cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    8/35

    Slide 8

    When the action is more than

    one statement

    Put multiple action statements within braces

    if

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    9/35

    Slide 9

    Example: Absolute Value (2nd)// program to read number & print its absolute value

    #include

    int main(){

    int value;

    int absvalue; // absolute value

    cout > value;

    if (value < 0) {

    absvalue = -value;

    cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    10/35

    Slide 10

    How to create an

    expression for a test?

    Using relational operators

    Using boolean (logical) operators

    An expression for a test (a boolean expression) has one of

    the two values: true or false.

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    11/35

    Slide 11

    Using Relational Operators

    Relational operators are used to compare two values

    Math C++ Plain English

    = == equals [example: if(a==b) ]

    [ (a=b) means put the value ofb into a ]< < less than

    > greater than

    >= greater than or equal to

    != not equal to

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    12/35

    Slide 12

    Relational Expressions

    Examples:

    numberOfStudents < 200

    10 > 20

    20 * j == 10 + i

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    13/35

    Slide 13

    Using Boolean (logical)

    operators

    Logical AND operator && Logical OR operator || Logical NOT operator !

    Examples: (x>5) && (x10) || (x5)

    Warning!

    & and | are also operators

    Boolean operators can be used to form more complexconditional expressions

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    14/35

    Slide 14

    A Boolean Type

    Example logical expressions

    bool P = true;

    bool Q = false;

    bool R = true;

    bool S = P && Q;

    bool T = !Q || R;

    bool U = !(R && !Q);

    C++ contains a type (new!!!) named bool

    which can have one of two values

    true (corresponds to non-zero value)

    false (corresponds to zero value)

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    15/35

    Slide 15

    Operator Precedence

    Which comes first?

    ( )

    * / %

    + -

    < = >

    == !=

    =

    Answer:

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    16/35

    Slide 16

    Summary of Operator

    Precedence

    Precedence of operators (from highest to lowest) Parentheses ( )

    Unary operators !

    Multiplicative operators * / %

    Additive operators + -

    Relational ordering < = >

    Relational equality == !=

    Logical and &&

    Logical or ||

    Assignment =

    arithmetic

    relational

    logical

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    17/35

    Slide 17

    5 != 6 || 7

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    18/35

    Slide 18

    Boolean expressions

    Arithmetic expression: use arithmetic operators +,-

    ,*,/, to produce a number as the final result

    Boolean expression: use relational operators , ==,

    and boolean operators AND (&&), OR (||), NOT (!)to produce one of the two values true (1) and false

    (0) as the final result

    New type: bool, true, false

    Example: bool cond;

    cond = true;cond = (x>y);

    Old versions of C++, simulated boolean type by int with 0/1

    If (BOOLEAN EXPRESSION)

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    19/35

    Slide 19

    Alternative ways of

    writing choice statements

    If statement

    Nested if

    If-else-if statement

    Switch statement

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    20/35

    Slide 20

    The if Statement

    Syntax

    if(Expression)

    Action

    If the Expression is true

    then execute Action Action is either a single

    statement or a group ofstatements within braces

    Example: absolute value

    if(value < 0)

    value = -value;

    Expression

    Action

    true false

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    21/35

    Slide 21

    Example: Absolute Value (3rd )

    // program to read number & print its absolute value

    #include

    int main(){

    int value;

    cout > value;

    if(value < 0)

    value = -value;

    cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    22/35

    Slide 22

    Example: Sorting Two

    Numbers

    int value1;

    int value2;

    int temp;

    cout > value1 >> value2;

    if(value1 > value2){

    temp = value1;

    value1 = value2;

    value2 = temp;

    }cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    23/35

    Slide 23

    Nested if Statements

    Nested means that one complete statement is insideanother

    if {

    if {

    if {

    }

    }

    }

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    24/35

    Slide 24

    Nested if Statements

    Example:

    if {

    if

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    25/35

    Slide 25

    if-else-if Statements

    if {

    }

    else if {

    }

    else if {

    }

    else{

    }

    Q

    R

    TS

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    26/35

    Slide 26

    if {

    }

    else if {

    }

    else if {

    }

    else{

    }

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    27/35

    Slide 27

    Selection

    Often we want to perform a particular actiondepending on the value of an expression

    Two ways to do this

    if-else-if statementif-else statements glued together

    switch statement

    An advanced construct

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    28/35

    Slide 28

    switch StatementAn alternative way of writing nested if-else-if statements for multiple choic

    switch (Expression) {

    case constant_1: action1; break;

    case constant_2: action2; break;

    case constant_n: actionN; break;

    default: actionDefault

    }

    if Exp==constant_1 {

    action1;

    }

    else if Exp==constant_2 {

    action2;}

    else if {

    }

    else{

    actionDefault

    }

    =

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    29/35

    Slide 29

    if(score >= 90)cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    30/35

    Slide 30

    Example using switch:

    switch(int(score)/10){

    case 10:

    case 9: cout

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    31/35

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    32/35

    Slide 32

    Dangling Else Problem

    Problem: Nested if statements can seemambiguous in their meaning.

    What is the value of c after the following is

    executed?int a=-1, b=1, c=1;

    if(a>0)

    if(b>0)

    c = 2;

    else

    c = 3;

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    33/35

    Slide 33

    C++ groups a dangling else with the most

    recent if. The following indentation shows how C++

    would group this example (answer: c=1).

    int a=-1, b=1, c=1;

    if(a>0)

    if(b>0)

    c = 2;

    else // dangling else grouped to nearest if

    c = 3;

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    34/35

    Slide 34

    Use extra brackets { } to clarify the intended

    meaning, even if not necessary.

    int a=-1, b=1, c=1;

    if(a>0){

    if(b>0)

    c = 2;

    else // parenthesis avoid dangling else

    c = 3;

    }

  • 8/3/2019 BC101 Lecture 4 - Program Structures

    35/35

    Slide 35

    Summary on condition

    statement The fundamental If-else statement

    if, nested if, if-else-if, switch

    Bool type and boolean expression for a test

    true/false

    relational operators (==,