Arithmetic Expressions - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Arithmetic Expressions

Description:

Each expression can be evaluated to compute a value of a given type ... ternary involving 3 operands later. CS 1284 Introduction to Computer Programming ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 21
Provided by: bridges
Category:

less

Transcript and Presenter's Notes

Title: Arithmetic Expressions


1
Arithmetic Expressions
  • Reading Chapter 2

2
Arithmetic Expression
  • Anything reducible to a single value
  • Consists of constants and/or variables connected
    by arithmetic operators
  • Each expression can be evaluated to compute a
    value of a given type

3
Operator Types
  • binary involving 2 operands 2 3
  • unary involving 1 operand -3
  • ternary involving 3 operands later

4
Precedence/Associativity
  • Precedence determines which operator is applied
    first in an expression having several operators.
  • Associativity dictates the order if two operators
    have the same precedence.
  • left to right the left operator applied first
  • right to left the right operator applied first

5
Precedence/Associativity of Arithmetic Operators
6
Example
2
1
6
5
3
7
4
x 5 6 4 - 2 6 / 3 10 3
x 5 24 - 2 6 / 3 10 3
x 5 24 - 12 / 3 10 3
x 5 24 - 4 10 3
x 5 24 - 4 1
x 29 - 4 1
x 25 1
x 26
7
Parentheses
  • Parentheses can be used to change the usual
    order.
  • Parts in ( ) are evaluated first.

8
Example
  • Handwritten Expression
  • In C (Without Parentheses)
  • 6 4 / 2 4 / 4 3 2 / 2 ? 1 10
  • In C (With Parentheses)
  • ( (6 4) / 2 4) / (4 3 2 / (2 ? 1) )
    2

9
Integer Arithmetic
  • If both operands of a binary operator are
    integers, the result is also an integer type.
  • Be careful that the result is not too big
  • short x
  • x 3200010 // x can be 7680
  • Division truncates any fractions
  • 3 / 2 1 3 / 2 ? 1.5

10
Integer Arithmetic (Cont..)
  • The remainder (modulus) operator
  • can only be used with integer type operands
  • always has an integer type result
  • 14 10 4
  • 3 10 3

11
Mixed Arithmetic
  • Result is the higher data type used
  • Higher data type takes most memory
  • Floats are higher than Integers

8.3 5 / 2
8.3 5 / 2.0
2 (int)
2.5 (double)
10.3 (double)
10.8 (double)
12
Increment/Decrement Operators
  • Increment uses
  • value or value
  • equivalent to
  • value value 1
  • Decrement uses --
  • value-- or --value
  • equivalent to
  • value value - 1

13
Increment/Decrement Operators (Cont ..)
  • Increment and decrement can appear as a prefix or
    a postfix
  • Prefix increment/decrement
  • Changes before the expression
  • value or --value
  • Postfix increment/decrement
  • Changes after the expression
  • value or value--

14
Prefix or Postfix?
  • If the increment (or decrement) operator is used
    in a stand alone statement solely to add one
    (or subtract one) from a variables value, it can
    be used in either prefix or postfix form.
  • If the increment (or decrement) operator is used
    in a statement with other operators, the prefix
    and postfix forms can yield different results.
  • Well see how later . . .

15
Assignment
  • Form of variable expression
  • The expression on right is evaluated first.
  • The resulting value is then stored in the memory
    location of variable on left.
  • An automatic type coercion occurs after
    evaluation but before the value is stored if the
    types differ for expression and variable.
  • A variable may have only one value at a time
  • Associativity of allows multiples
  • x y z 17 9

16
Type Conversion
  • There can be two kinds of conversions of a value
    from one data type to another
  • Implicit conversion (type coercion)
  • Explicit (type casting)

17
Implicit conversion
  • Any value may be assigned to any type of
    variable.
  • Implicit (automatic) conversion happens when
    assigning values to different data types
  • int someInt
  • float someFloat
  • someFloat 12 // 12 is converted to 12.0
  • someInt 4.8 // fractional part is dropped
  • Avoid implicit conversion

18
Explicit conversion
  • Good programmers do not depend on implicit
    conversions
  • Explicit conversion is done with a cast operation
  • The cast operation consists of the desired data
    type and then, within parentheses, the expression
    to be converted
  • int diameter 9
  • float radius
  • radius float(diameter) / 2

19
Example
  • int(4.8) has value 4
  • float(5) has value 5.0
  • float(7/4) has value 1.0
  • float(7)/float(4) has value 1.75

20
Example
  • float loCost
  • float hiCost
  • loCost 12.342
  • hiCost 12.348
  • loCost float(int(loCost100.00.5))/100.0
  • hiCost float(int(hiCost100.00.5))/100.0
Write a Comment
User Comments (0)
About PowerShow.com