Operators & Mathematical Expression

download Operators & Mathematical Expression

of 27

Transcript of Operators & Mathematical Expression

  • 8/18/2019 Operators & Mathematical Expression

    1/27

    Operators and

    MathematicalExpressions

  • 8/18/2019 Operators & Mathematical Expression

    2/27

    Mathematical Expressions

    An expression can be a constant, avariable, or a combination of constantsand variables combined with operators.

    Can create complex expressions usingmultiple mathematical operators.

    Example mathematical expressions

    2heighta + b / c 

  • 8/18/2019 Operators & Mathematical Expression

    3/27

    Using Mathematical Expressions

    Can be used in assignment statements,with cout, and in other types of

    statements Examples:

    area = 2 * PI * radius;

    cout

  • 8/18/2019 Operators & Mathematical Expression

    4/27

    Assignment tatement

    Uses the e!ual sign = operator "as a single variable on the left side and

    a value on the right side Copies the value on the right into thevariable on the leftx = 5; = !; = #;

    x = x + ; = + #;

    = 5 + x + ;

  • 8/18/2019 Operators & Mathematical Expression

    5/27

    #perators

     $hese are the symbols that tells thecompiler to perform a speci%c

    mathematical or logical manipulation.Types of Operators:

    &. Arithmetic

    '. (elational). *ogical

  • 8/18/2019 Operators & Mathematical Expression

    6/27

    Arithmetic #perators

    SYMBOL OPERATION EXAMPLE a$s

    +   addition a$s = % + &; '

      subtraction a$s = % &; #

    *   multiplication a$s = % * &; 2'

    /  division

    a$s = % / &; 2

      modulus a$s = % &; '

  • 8/18/2019 Operators & Mathematical Expression

    7/27

    +recedence of #perators

  • 8/18/2019 Operators & Mathematical Expression

    8/27

    Associativity of #perators

     unary negation- associates right to left *  /    +    all associate left to right parentheses ( ) can be used to override the

    order of operations

    2 + 2 * 2 2 = #

     (2 + 2) * 2 2 = !

      2 + 2 * (2 2) = 2

     (2 + 2) * (2 2) =

  • 8/18/2019 Operators & Mathematical Expression

    9/27

  • 8/18/2019 Operators & Mathematical Expression

    10/27

    7ivision 4- #perator

    C00 division operator /- performs integerdivision if both operands are integers

    cout

  • 8/18/2019 Operators & Mathematical Expression

    11/27

     Modulus - #perator

    C00 modulus operator %- computes theremainder resulting from integer division

    cout

  • 8/18/2019 Operators & Mathematical Expression

    12/27

    Algebraic Expressions

    Multiplication re!uires an operator

     Area = lw  is written as ,rea = l * w;

     $here is no exponentiation operator Area = s2  is written as ,rea = -ow(s.2);

    +arentheses may be needed to maintainorder of operations

    is written as

      = (2')/(x2x');12

    12

     x x

     y ym

    =

  • 8/18/2019 Operators & Mathematical Expression

    13/27

    Mathematical 'l(e)raic

    Expression

    Mathematical 'l(e)raic

    Expression in *++

    a)c a , ) , c

    a + )c a + ) , c

    y3 po- .y 30 or y , y , y

    a

    ) + c 

    a / .)+c0

    a.a+)0 a , .a + )0

  • 8/18/2019 Operators & Mathematical Expression

    14/27

    ome +rede%ned ;unctions inC00

    +rede%ned functions 6 are functions thatare built into C00 to perform somestandard operations.

     $hese functions were prede%ned for you,meaning that you didn

  • 8/18/2019 Operators & Mathematical Expression

    15/27

    ome +rede%ned ;unctions inC00

     $he header %le re!uired formathematical functions is 0i$clude

     

  • 8/18/2019 Operators & Mathematical Expression

    16/27

    (elational #perators

    Used to compare numbers todetermine relative order

    (elational #perators:1  Greater than

     < Less than

    1= Greater than or equal to

     

  • 8/18/2019 Operators & Mathematical Expression

    17/27

    (elational Expressions

    (elational expressions are >ooleani.e., evaluate to true or 3alse)

    Examples:'2 1 5 is true%

  • 8/18/2019 Operators & Mathematical Expression

    18/27

    (elational Expressions

    Assigns  for 3alse, ' for true

    7o not confuse =  and ==

  • 8/18/2019 Operators & Mathematical Expression

    19/27

    *ogical #perators

    Used to create relational expressionsfrom other relational expressions.ometimes referred to as >oolean

    operators.

        AND New relational expression is true if bothexpressions are true

     66   OR New relational expression is true if eitherexpression is true

        NOTReverses the value of an expression;true expression becomes false, andfalse becomes true

    7o(ical Operators:

  • 8/18/2019 Operators & Mathematical Expression

    20/27

     $ruth $able for the 99 logical A?7-operator

    alue ofExpression1

    alue ofExpression

    alue ofExpression1 99

    Expression

     $rue $rue $rue

     $rue ;alse ;alse;alse $rue ;alse

    ;alse ;alse ;alse

  • 8/18/2019 Operators & Mathematical Expression

    21/27

     $ruth $able for the  logical #(-#perator

    alue ofExpression

    1

    alue ofExpression

    alue ofExpression

    1 Expression

     $rue $rue $rue

     $rue ;alse $rue

    ;alse $rue $rue

    ;alse ;alse ;alse

  • 8/18/2019 Operators & Mathematical Expression

    22/27

    *ogical #perator Examples

    i$t x = '2. = 5. = #;

     

    (x 1 ) ( 1 ) true

    (x 1 ) ( 1 ) 3alse

    (x

  • 8/18/2019 Operators & Mathematical Expression

    23/27

    *ogical +recedence

      8i(hest   

      7o-est 66

      Example:  (2 < &) 66 (5 1 !) (% 1 4)

      is true because A?7 is done before #( 

  • 8/18/2019 Operators & Mathematical Expression

    24/27

    More on +recedence

    Example:

      = @ ' 0 BB / 5 is true

     

    Hi!"st  Arithmetic Operators

    Relational Operators

    Lo#"st Loical Operators

  • 8/18/2019 Operators & Mathematical Expression

    25/27

    nowledge ChecD

    Evaluate the following logical expressions:

    a&/, b'3, c6/ d&3

    &. b d FF cG6/ BB c &33'. a &/ FF &'3 @ 3

    ). G )3 G b- BB c6/ FF 50/ d

  • 8/18/2019 Operators & Mathematical Expression

    26/27

    That in 'll

    Thin(s, od May eloriedG

  • 8/18/2019 Operators & Mathematical Expression

    27/27

     $hanD youG

    +repared by: +rof. *(I ?atividad