Top Banner

of 44

03 Expressions Statements Control Flow

Aug 08, 2018

Download

Documents

Paul Tacuel
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/22/2019 03 Expressions Statements Control Flow

    1/44

    Statements, Expressions and

    Control Flow

  • 8/22/2019 03 Expressions Statements Control Flow

    2/44

    controls the sequence of execution,evaluates an expression, or does nothing(the null statement)

    Basically all lines of code

    What is a statement?

  • 8/22/2019 03 Expressions Statements Control Flow

    3/44

    Any place you can put a single statement,you can put a compound statement, alsocalled a block.

    Non-strictly speaking, we can say that ablock of code acts as one statement.

    Block or compound statements

  • 8/22/2019 03 Expressions Statements Control Flow

    4/44

    Anything that evaluates to a value.Expressions are said to return a value.

    3.2

    PI

    SecondsPerMinute

    x = a + b; return value is x

    y = x = a + b; return value is y

    What is an expression?

  • 8/22/2019 03 Expressions Statements Control Flow

    5/44

    a symbol that causes the compiler to takean action

    Operators act on operands. All operands

    are expressions.

    Operators

  • 8/22/2019 03 Expressions Statements Control Flow

    6/44

    = x = a + b;

    lvalue term used for an operand thatcan be on the left side of an assignmentoperator

    rvalue ?

    Example: x = 35; 35 = x; // error, 35 not an lvalue

    Assignment operator

  • 8/22/2019 03 Expressions Statements Control Flow

    7/44

    +, -, *, /, %(modulo)

    Some notes:

    subtraction of unsigned integers that lead tonegative values can produce unpredictable results

    beware of division!

    23/4 5

    23.0/4 5.75

    Mathematical Operators

  • 8/22/2019 03 Expressions Statements Control Flow

    8/44

    you can put the same variable on the sameside of the assignment operator myVal = myVal + 2;

    you can also use a shorthand notation(called the self-assigned additionoperator): myVal += 2;

    myVal /= 2;

    Notes on assignment and math operators

  • 8/22/2019 03 Expressions Statements Control Flow

    9/44

    c++; c = c + 1;

    c +=1;

    c--;

    Note: do not use increment or decrementas subexpressions of a larger expression: ((n+2) * (++n)) + 5;

    Increment and decrement

  • 8/22/2019 03 Expressions Statements Control Flow

    10/44

    postfix: x++;

    prefix: ++x;

    When value is not assigned to another

    variable no effect. int x = 6, y = 6;

    int a = ++x; // a = x = x+1

    int b = y++; // b = y; y = y + 1;

    The value of x and y is of course 7. a = 7, b = 6

    prefix and postfix

  • 8/22/2019 03 Expressions Statements Control Flow

    11/44

    *, /, % +, -

    =, +=, -=, *=, /=, %=

    Use parentheses to control precedence

    Precedence

  • 8/22/2019 03 Expressions Statements Control Flow

    12/44

    Not-so-good: EffortSpent = (((NumDays NumWeekends)

    * 24)*(NumSupervisors + NumEngrs));

    Better:

    ActualDays = NumDays NumWeekends;

    ActualHours = ActualDays * 24;

    NumPeople = NumSupervisors + NumEngrs;

    EffortSpent = ActualHours * NumPeople;

    Note on Nesting parentheses

  • 8/22/2019 03 Expressions Statements Control Flow

    13/44

    In C++, zero is considered false, all othervalues are considered true.

    Note: there is a data type bool whosevalue can only be TRUE or FALSE.

    Program flow is controlled by whether acertain condition is TRUE or FALSE.

    Truth

  • 8/22/2019 03 Expressions Statements Control Flow

    14/44

    used to compare whether two numbersare equal, or if one is greater or less thanthe other.

    equals ==

    not equals !=

    greater than >

    greater than or equals >=

    less than y and y>z?

    AND && (x > y) && (y > z)x>y and y>z

    OR ||(x > y) || (y > z)

    (x > y) or (y > z)

    NOT ! !(x > y)is it NOT true that x > y?

    Logical operators

  • 8/22/2019 03 Expressions Statements Control Flow

    18/44

    NOT Mathematical

    Relational

    AND OR

    Assignment

    More precedence

  • 8/22/2019 03 Expressions Statements Control Flow

    19/44

    Machine Problem #2

    Deadline: June 28, 2010

  • 8/22/2019 03 Expressions Statements Control Flow

    20/44

    enables us to branch to different parts ofour code by testing for conditions

    if (expression)

    statement;

    note that a block can replace anystatement:

    if (expression){

    statement1;

    statement2;

    }

    IF

  • 8/22/2019 03 Expressions Statements Control Flow

    21/44

    Aif (expression) {

    statements

    }

    B

    if (expression)

    {statements

    }

    C

    if (expression)

    {

    statements}

    Indentation styles

  • 8/22/2019 03 Expressions Statements Control Flow

    22/44

    if (expression){ statement;

    }

    else { statement;

    }

    ELSE

  • 8/22/2019 03 Expressions Statements Control Flow

    23/44

    if (expression1) { if (expression2)

    statement1;

    else {

    if (expression3)

    statement2; else

    statement3;

    }

    }

    else statement4;

    Nested IFs

  • 8/22/2019 03 Expressions Statements Control Flow

    24/44

    It can cause by difficult to debug run-timeerrors

    if (x > 10)

    if (x > 100)

    cout

  • 8/22/2019 03 Expressions Statements Control Flow

    25/44

    if (expression1)

    statement1;

    else if (expression2)

    statement2;

    else if (expression3)

    statement3;

    else

    statement4;

    ELSE IF

  • 8/22/2019 03 Expressions Statements Control Flow

    26/44

    the only ternary operator; i.e. onlyoperator with 3 terms.

    (expression1) ? (expression2) :

    (expression3)

    this reads: if expression1 is true, returnthe value of expression2; else, return thevalue of expression3

    absValue = a>b ? a-b : b-a;

    Conditional Operator

  • 8/22/2019 03 Expressions Statements Control Flow

    27/44

    Write a program that outputs High if the value of thevariable is greater than 10, and Low if otherwise. Thevariable is of type int and is inputted by the user.

    Write a program that obtains input from the user ahigh_value, a low_value and a value from the user. Yourprogram should inform the user if the value is out of range!;otherwise, output thank you!

    Enter high value: 10

    Enter low value: 3

    Enter your choice: 2

    value out of range!

    Write a program similar to number 1 except that it will inform

    the user of the 3 following possibilities: a) number

  • 8/22/2019 03 Expressions Statements Control Flow

    28/44

    Write a program that computes the absolutevalue of the difference of two user inputtedintegers.

    Exercises

  • 8/22/2019 03 Expressions Statements Control Flow

    29/44

    similar to the IF statement. allows multi-way branches.

    it is less flexible than IF-ELSE becauseswitch statements use constants.

    switch

  • 8/22/2019 03 Expressions Statements Control Flow

    30/44

    switch (controlling_expression) { case constant1:

    statement_sequence_1;

    break;

    case constant2:

    statement_sequence_2;

    break;

    default:

    default_statement_sequence; }

    switch

  • 8/22/2019 03 Expressions Statements Control Flow

    31/44

    if you forget a break statement, your codewill continue until a break statement fromanother case statement is reached or untilthe end of the switch statement

    case A:

    case a:

    cout

  • 8/22/2019 03 Expressions Statements Control Flow

    32/44

    each loop is called an iteration while

    for

    do-while

    Looping

    hil

  • 8/22/2019 03 Expressions Statements Control Flow

    33/44

    while (boolean_expression) { statement_1;

    statement_2;

    }

    while

    hil

  • 8/22/2019 03 Expressions Statements Control Flow

    34/44

    example:int numOfGreetings;

    cin >> numOfGreetings;

    while(numOfGreetings > 0) {

    cout

  • 8/22/2019 03 Expressions Statements Control Flow

    35/44

    do { statement_1;

    statement_2;

    } while (boolean_expression);

    do - while

    d hil

  • 8/22/2019 03 Expressions Statements Control Flow

    36/44

    example:int numOfGreetings;

    cin >> numOfGreetings;

    do {

    cout 0) ;

    cout

  • 8/22/2019 03 Expressions Statements Control Flow

    37/44

    for (initialization; boolean_expr; update) {

    statement_1;

    statement_2;

    ... }

    for

    f

  • 8/22/2019 03 Expressions Statements Control Flow

    38/44

    example:int numOfGreetings;

    cin >> numOfGreetings;

    for (; numOfGreetings > 0; numOfGreetings--

    ){

    cout

  • 8/22/2019 03 Expressions Statements Control Flow

    39/44

    int numOfGreetings; cin >> numOfGreetings;

    for (int i=0; i

  • 8/22/2019 03 Expressions Statements Control Flow

    40/44

    when exit conditions are not satisfied,the program continues to loop forever

    your program will hang

    Infinite loops

    break and continue

  • 8/22/2019 03 Expressions Statements Control Flow

    41/44

    used to alter the flows of while, do-whileand for statements

    break

    ends the nearest enclosing loop

    continue

    ends the current loop iteration of the nearestenclosing loop

    break and continue

    Practice exercises

  • 8/22/2019 03 Expressions Statements Control Flow

    42/44

    Write a program that finds and prints all of theprime numbers between 3 and 100.

    Write a program which takes a single integer fromthe user and displays a "pyramid" of this height(between 1-30) made up of of "*" characters on thescreen.

    6:**

    ****

    ******

    ********

    **********************

    Practice exercises

    Exercises

  • 8/22/2019 03 Expressions Statements Control Flow

    43/44

    The LeVan Car Rental Company charges30PhP/km if the total mileage is less than100km. If the total mileage is over 100, thecompany charges 30PhP/km for the first 100

    km, then it charges 20PhP for any additionalkm over 100. The company gives a 500PhPdiscount for total mileage more than 500km.

    Write a program so that the clerk has to input

    the number of kilometers and the programwill output the total price

    Exercises

    READING ASSIGNMENT

  • 8/22/2019 03 Expressions Statements Control Flow

    44/44

    Functions

    READING ASSIGNMENT