P5 Operators and Expressions

download P5 Operators and Expressions

of 25

Transcript of P5 Operators and Expressions

  • 8/3/2019 P5 Operators and Expressions

    1/25

  • 8/3/2019 P5 Operators and Expressions

    2/25

    OPERATOROPERATOR

    Symbol that operates on a certain data

    type.

    Example:+ -addition operator

    Can operate on integer, character and real

    Expression-combination of variables,constants and operators

    Every expression evaluates to a value

  • 8/3/2019 P5 Operators and Expressions

    3/25

    CONCEPT OF EVALUATIONCONCEPT OF EVALUATION

    = Assignment operator

    Assigns the value of the variable on the left hand

    side to that of the right hand side.

    Left hand side of the assignment operator and

    the right hand side together form an expression.

    For example:

    int i,j;Valid expression would be: i=5;

    j=i=5;

  • 8/3/2019 P5 Operators and Expressions

    4/25

    TYPES OF OPERATORSTYPES OF OPERATORS

    Classified based on their utility and action.

    Arithmetic operators

    Relational operators

    Logical operators

  • 8/3/2019 P5 Operators and Expressions

    5/25

    ARITHMETIC OPERATORSARITHMETIC OPERATORS

    Perform arithmetic operations

    Operates on any data type

    List of arithmetic operators+ - addition

    - - subtraction

    * - multiplication

    / - division

    % - modulo division

  • 8/3/2019 P5 Operators and Expressions

    6/25

    Integer ArithmeticInteger Arithmetic

    If operands such as x and y are integers,

    then arithmetic operation on them is called

    integer arithmetic. Always yields an integer value.

    Example:

    int x=16,y=5; Integer arithmetic on x and y are as

    follows:

  • 8/3/2019 P5 Operators and Expressions

    7/25

    Integer ArithmeticInteger Arithmetic

    x=16,y=5;

    x + y = 21;

    x y = 11;

    x * y = 80;

    x / y = 3;/*result is truncated, decimal part is

    discarded*/

    x % y =1;/*result is the remainder of the integerdivision, sign of the result=sign of the

    first operand*/

  • 8/3/2019 P5 Operators and Expressions

    8/25

    Integer DivisionInteger Division

    Result is truncated towards zero if both the

    operands are of the same sign, and is

    dependent on the machine if one of the

    operands is negative.

    Example:

    6 / 8 = 0;

    -6 / -8 = 0;

    -6 / 8 = 0 or -1 /*machine dependent */

  • 8/3/2019 P5 Operators and Expressions

    9/25

    Floating point ArithmeticFloating point Arithmetic

    Operands are of data type float

    % operator is not applicable

    Example:float a=14.0,b=4.0;

    P = a / b = 3.500000

    Q = b / a = 0.285714R = a + b = 18.00000

  • 8/3/2019 P5 Operators and Expressions

    10/25

    Mixed Mode ArithmeticMixed Mode Arithmetic

    Operation between an integer and real

    always yields a real result.

    In this operation the integer is firstpromoted to a real and then the operation

    is performed. Hence the result is real.

  • 8/3/2019 P5 Operators and Expressions

    11/25

    Mixed Mode ArithmeticMixed Mode Arithmetic

  • 8/3/2019 P5 Operators and Expressions

    12/25

    Relational OperatorsRelational Operators

    Used to compare

    arithmetic, logical and

    character

    expressions. Condition is evaluated

    zero- false

    one - true.

    Operator Meaning

    < Less than

    > Greater than

    = Greater than

    or equal to== Equal to

    != Not equal to

  • 8/3/2019 P5 Operators and Expressions

    13/25

    Logical OperatorsLogical Operators

    Used to compare or evaluate logical and

    relational expressions.

    Operator Meaning

    && Logical AND

    !! Logical OR! Logical NOT

  • 8/3/2019 P5 Operators and Expressions

    14/25

    Type conversion in assignmentsType conversion in assignments

    Sometimes type of the variable on the left

    hand side may not be the same.

    Value is promoted or demoted dependingon the value on the left hand side of =.

    Example

    int i ;

    float b ;

    i = 3.5 ; /*Float is demoted to int and only 3 is stored*/

    b = 30 ; /*30 is promoted to float and 30.0000 is stored*/

  • 8/3/2019 P5 Operators and Expressions

    15/25

    Type conversion in assignmentsType conversion in assignments

    Let us consider an complex instruction

    float a, b, c ;

    int s ;

    s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ; /* answer value of float type is

    demoted to int and stored in s */

    K is an integer

    A is an real

  • 8/3/2019 P5 Operators and Expressions

    16/25

    Hierarchy of OperationsHierarchy of Operations

    Priority or precedence in which operations are

    performed arithmetic statements.

    Within parenthesis hierarchy is same.But,if there is more than one set ofparenthesis, innermost parenthesis would be performed first.

  • 8/3/2019 P5 Operators and Expressions

    17/25

    Hierarchy of OperationsHierarchy of Operations

    Determine hierarchy and evaluate the

    following expression

    i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8

    i = 6 / 4 + 4 / 4 + 8 - 2 + 5/ 8 operation: *

    i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /

    i = 1 + 1+ 8 - 2 + 5 / 8 operation: /

    i = 1 + 1 + 8 - 2 + 0 operation: /

    i = 2 + 8 - 2 + 0 operation: +

    i = 10 - 2 + 0 operation: +i = 8 + 0 operation : -

    i = 8 operation: +

  • 8/3/2019 P5 Operators and Expressions

    18/25

    Hierarchy of OperationsHierarchy of Operations

    Evaluate

    kk = 3 / 2 * 4 + 3 / 8 + 3

    kk = 1 * 4 + 3 / 8 + 3 operation: /kk = 4 + 3 / 8 + 3 operation: *

    kk = 4 + 0 + 3 operation: /

    kk = 4 + 3 operation: +kk = 7 operation: +

  • 8/3/2019 P5 Operators and Expressions

    19/25

    Converting General ArithmeticConverting General Arithmetic

    Statement to C StatementStatement to C Statement

  • 8/3/2019 P5 Operators and Expressions

    20/25

    Associativity of operatorsAssociativity of operators

    Expression contains two operators of equalpriority, then it is solved using Associativity.

    It can be of two types.

    Left to Right

    - Left operand must be unambiguous

    Right to Left- Right operand must be

    unambiguous

  • 8/3/2019 P5 Operators and Expressions

    21/25

    Associativity of operatorsAssociativity of operators

    Consider the expression

    a = 3 / 2 * 5 ; /* there is a tie between *

    and / operator */

  • 8/3/2019 P5 Operators and Expressions

    22/25

    Associativity of operatorsAssociativity of operators

    Consider one more expression

    a = b = 3 ; /* both = has the same priority*/

  • 8/3/2019 P5 Operators and Expressions

    23/25

    Associativity of operatorsAssociativity of operators

    Another expression

    z = a * b + c / d ; /* / and * has same priority

    and same Associativity */

  • 8/3/2019 P5 Operators and Expressions

    24/25

    I request Electronics and communication

    ENGINEERING students to visit my blogfor

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.com

  • 8/3/2019 P5 Operators and Expressions

    25/25