programming chapter 2 week 3

Upload: shaun-stanley

Post on 04-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Programming Chapter 2 Week 3

    1/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 1

    Chapter 2

    Operation and Expression

  • 8/13/2019 Programming Chapter 2 Week 3

    2/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 2

    Learn how C computes data

    TOPICObjectives:

    TOPIC Objectives:

  • 8/13/2019 Programming Chapter 2 Week 3

    3/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 3

    Symbol Meaning Example Result

    + Addition 2 + 3 5

    - Subtraction 5 - 3 2* Multiplication 4 * 2 8

    / Division 10 / 2.0 5

    % Modulus 5 % 2 1

    2.1MathOperatorsandPrecedence

    2.1 Math

    Operators and Precedence

    Primary Math OperatorsPrimary Math Operators

  • 8/13/2019 Programming Chapter 2 Week 3

    4/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 4

    Note:

    If the modulus %sign is needed to be displayed as part of a text string, use

    two, ie %%

    #include

    main()

    {

    intsum = 50;floatmodulus;

    modulus = sum % 10;

    printf("The %% of %d by 10 is %f\n", sum, modulus);

    }

    Sample Program OutputThe % of 50 by 10 is 0.000000

    What does the following change do to the printed output of the previous program?

    printf("The %% of %d by 10 is %.2f\n", sum, modulus);

  • 8/13/2019 Programming Chapter 2 Week 3

    5/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 5

    Example of expression:

    Order of Precedence

    First: { *, / , % }

    Second: { +, - }

    Order of PrecedenceOrder of Precedence

    6 + 2 * 3 - 4 / 2

    6 + 6 - 4 / 2

    6 + 6 - 2

    12 - 2

    10

    Also called math hierarchy Determine exactly how C computes formulas

  • 8/13/2019 Programming Chapter 2 Week 3

    6/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 6

    Used to override the order of precedence

    Example of

    expression:

    Average = ( first + second ) / 2.0;

    NOT

    Average = first + second / 2.0;

    ParenthesesParenthesesParentheses

    2 * 3 + 4

    6 + 4

    10

    2 * ( 3 + 4 )

    2 * 7

    14

    Note: Sometimes parentheses is must exist in expression.

    Example;An average of 2 numbers.

  • 8/13/2019 Programming Chapter 2 Week 3

    7/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 7

    Syntax:

    var iableName = Expressio n;

    Expression:

    Can be constants,variableor combination of constants

    and

    variables using math operators

    Examples of

    assignments

    statements:

    Value = 7;

    A = B;Y = 2 * X + 2;

    Assignment StatementAssignment StatementsAssignment Statements

    Refer: TOPIC 1 (1.5)

  • 8/13/2019 Programming Chapter 2 Week 3

    8/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 8

    Assign the same value to different variables

    Three

    assignments

    statements:

    A = 4;

    B = 4;

    C = 4;

    A = B = C = 4;

    What is the meaning ofA = B = C = D;

    Multiple assignment form:

    Multiple AssignmentMultiple AssignmentMultiple Assignments

    Sets all counter variables to zero:

    passed = 0;failed = 0;

    index =0;total = 0;

    passed=failed=index=total=0;

  • 8/13/2019 Programming Chapter 2 Week 3

    9/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 9

    Symbol Normal Compound

    Assignment Assignment

    += A = A + 2; A += 2;

    -= B = B - 3; B -= 3;

    *= C = C * D; C *= D;

    /= E = E / 2.5; E /= 2.5;

    %= F = F % 4; F %= 4;

    Compound AssignmentCompound AssignmentCompound Assignments

    Convert this compound assignment to normal assignment

    salary += (bonus + overtime - tax);

    Combination of math operatorsand assignment

  • 8/13/2019 Programming Chapter 2 Week 3

    10/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 10

    Temporarily change a variables data typefrom its defined data type to a

    new one when an expression have mixed data types

    Conversion may occurautomatically or by typecasting.

    Example of

    assignment

    statements:

    /* Automatic Conversion */

    int bonus= 150;

    float total,salary= 900.45;

    total =salary+ bonus;

    /* Typecast */

    total = salary + (float) bonus;

    Syntax:

    (data type) expression

    Integer & Float

    Float & Float

    Typecast

    TypecastTypecast

  • 8/13/2019 Programming Chapter 2 Week 3

    11/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 11

    Symbol Meaning

    == Equal to

    > Greater than

    = Greater than or equal to

  • 8/13/2019 Programming Chapter 2 Week 3

    12/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 12

    Assume that a program initializes four variables:

    int a=5,b=10,c=15,d=5;

    The following expressionsare then TRUE:

    1. a==d

    2. b< c

    3. c>a

    4. b>=a

    5. dc

    3. d< a

    4. d> a

    5. a!=d

    6. c

  • 8/13/2019 Programming Chapter 2 Week 3

    13/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 13

    2.3 Arithmetic Arithmetic calculations Use *for multiplication and /for division

    Integer division truncates remainder

    7/5evaluates to 1 Modulus operator(%) returns the remainder

    7%5evaluates to 2

  • 8/13/2019 Programming Chapter 2 Week 3

    14/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 14

    Operator precedence

    Some arithmetic operators act before others (i.e.,

    multiplication before addition)

    Use parenthesis when needed

    Example: Find the average of three variables a, b

    and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

  • 8/13/2019 Programming Chapter 2 Week 3

    15/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 15

    C opetrationArithmeticoperator

    Algebraicexpression

    C expression

    Addition + f + 7 f + 7

    Subtraction pc p - c

    Multiplication * bm b * m

    Division /or or

    xx y x y

    y x / y

    Remainder % rmods r % s

    Arithmetic operators.

  • 8/13/2019 Programming Chapter 2 Week 3

    16/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 16

    Common Programming Error An attempt to divide by zero is normally

    undefined on computer systems and

    generally results in a fatal error, i.e., anerror that causes the program to terminate

    immediately without having successfully

    performed its job. Nonfatal errors allowprograms to run to completion, often

    producing incorrect results.

  • 8/13/2019 Programming Chapter 2 Week 3

    17/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 17

    Precedence of arithmetic

    operators.

    Operator(s) Operation(s) Order of evaluation (precedence)

    ( ) Parentheses Evaluated first. If the parentheses arenested, the expression in the innermost pair isevaluated first. If there are several pairs of

    parentheses on the same level (i.e., not nested),they are evaluated left to right.

    */

    %

    MultiplicationDivisionRemainder

    Evaluated second. If there are several, they areevaluated left to right.

    +-

    AdditionSubtraction

    Evaluated last. If there are several, they areevaluated left to right.

  • 8/13/2019 Programming Chapter 2 Week 3

    18/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 18

    Order in which a second-degree

    polynomial is evaluated.

  • 8/13/2019 Programming Chapter 2 Week 3

    19/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 19

    Good Programming Practice

    Using redundant parentheses in

    complex arithmetic expressions

    can make the expressions clearer.

  • 8/13/2019 Programming Chapter 2 Week 3

    20/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 20

    2.4 Decision Making: Equality and

    Relational Operators Executable statements

    Perform actions (calculations, input/output of data)

    Perform decisions May want to print "pass"or "fail"given the value of a test grade

  • 8/13/2019 Programming Chapter 2 Week 3

    21/21

    FCS 0084 Programming Chapter 2

    Operation and Expression Week 3

    Management and Science University 21

    ifcontrol statement

    Simple version in this section, more detail later If a condition is true, then the body of the if

    statement executed

    0is false, non-zero is true

    Control always resumes after the ifstructure

    Keywords

    Special words reserved for C

    Cannot be used as identifiers or variable names