4. EXPRESSIONS - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

4. EXPRESSIONS

Description:

4. EXPRESSIONS Arithmetic Operators Arithmetic Operators Operator Precedence Operator Precedence Associativity Operator precedence rules alone aren t enough when an ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 17
Provided by: Ritwik3
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: 4. EXPRESSIONS


1
4. EXPRESSIONS
2
Arithmetic Operators
  • The arithmetic operators are
  • unary plus
  • - unary minus
  • addition
  • - subtraction
  • multiplication
  • / division
  • remainder (modulus)

3
Arithmetic Operators
  • The value of i j is the remainder when i is
    divided by j. For example, the value of 10 3 is
    1 the value of 12 4 is 0. The operator
    requires integer operands all other operators
    allow either integer or floating-point operands.
  • If both operands are integers, / truncates the
    result toward 0.
  • There is no exponentiation operator.
  • Must use pow function defined in ltmath.hgt

4
Operator Precedence
  • When an expression contains more than one
    operator, the meaning of the expression may not
    be immediately clear
  • Does i j k mean (i j) k or
  • i
    (j k) ?
  • In C, this potential ambiguity is resolved by
    operator precedence.
  • Precedence of the arithmetic operators
  • Highest - (unary)
  • /
  • Lowest - (binary)

5
Operator Precedence
  • Examples
  • i j k means i (j k)
  • -i -j means (-i) (-j)
  • i j / k means (i) (j / k)

6
Associativity
  • Operator precedence rules alone arent enough
    when an expression contains two or more operators
    at the same level of precedence. The
    associativity of the operators now comes into
    play.
  • The binary arithmetic operators are all left
    associative (they group from left to right) the
    unary operators are right associative.
  • Examples of associativity
  • i - j - k means (i - j) - k
  • i j / k means (i j) / k
  • i - j i k means (i - (j i)) k
  • - - i means -(-i)

7
Increment and Decrement Operators
  • The and -- operators increment and decrement
    variables.
  • Both operators have the same precedence as
    negation.
  • Either operator can be prefix or postfix
  • i
  • i
  • --i
  • i--
  • When used as a prefix operator, increments the
    variable before its value is fetched
  • i 1
  • printf("i is d\n", i) / prints "i is 2" /

8
Increment and Decrement Operators
  • When used as a postfix operator, increments
    the variable after its value is fetched
  • i 1
  • printf("i is d\n", i) / prints "i is 1" /
  • printf("i is d\n", i) / prints "i is 2" /
  • The -- operator has similar properties
  • i 1
  • printf("i is d\n", --i) / prints "i is 0" /
  • i 1
  • printf("i is d\n", i--) / prints "i is 1" /
  • printf("i is d\n", i) / prints "i is 0" /

9
Lvalues
  • The increment and decrement operators require an
    lvalue (pronounced Lvalue) as their operand.
  • An expression has an lvalue if it represents a
    storage location in computer memory.
  • Variables are lvalues, since a variable is just
    a name for a storage location.
  • Constants and most expressions containing
    operators are not lvalues, including the
    following
  • 12
  • i j
  • -i

10
Simple Assignment
  • One way to change the value of a variable is by
    assignment.
  • Simple assignment has the form v e, where v is
    an lvalue and e is an expression. Examples
  • i 5 / i is now 5 /
  • j i / j is now 5 /
  • k 10 i j / k is now 55 /
  • If v and e dont have the same type, then the
    value of e is converted to the type of v before
    assignment
  • int i
  • float f
  • i 72.99 / i is now 72 /
  • f 136 / f is now 136.0 /

11
Simple Assignment (Continued)
  • Assignment is a true operator, just like and
    . The value of v e is the value of v after the
    assignment.
  • Assignments can be chained together
  • i j k 0
  • Since the operator is right associative, this
    statement is
  • equivalent to
  • i (j (k 0))
  • The assignment operator may appear in any
    expression
  • i 1
  • k 1 (j i)
  • printf("d d d\n", i, j, k) / prints "1 1 2"
    /
  • Hiding an assignment inside a larger expression
    usually isnt a good idea.

12
Compound Assignment
  • In addition to the simple assignment operator ,
    C provides ten compound assignment operators,
    including these five
  • - /
  • The expression v e is equivalent to
  • v v e,
  • except that v is evaluated only once in the
    former expression. The other operators have
    similar meanings.

13
Compound Assignment
Examples i 5 j 2 i j / i is now 7
/ i 5 j 2 i - j / i is now 3 / i
5 j 2 i j / i is now 10 / i 5 j
2 i / j / i is now 2 / i 5 j 2 i
j / i is now 1 /
14
Compound Assignment
The compound assignment operators have the same
properties as the operator in particular,
theyre right associative and can be used in
arbitrary expressions.
15
Expression Statements
  • Any expression can be used as a statement
  • i
  • The value of the expression is discarded the
    statement has no effect unless the expression
    modifies one of its operands
  • i 1 / stores 1 into i, then evaluates i
    and discard the result /
  • i-- / fetches i, discards the result,
    then decrements i /
  • i j / adds i and j, then discards the
    result /

16
Expression Statements
  • Expression statements are typically used to
    perform assignments or to increment or decrement
    variables.
  • Warning A minor typing mistake can turn a
    meaningful statement into a meaningless
    expression statement. For example, the statement
  • i j
  • could easily become
  • i j
Write a Comment
User Comments (0)
About PowerShow.com