Object-Oriented Programming Using C Third Edition - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Object-Oriented Programming Using C Third Edition

Description:

Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions Objectives Use C++ binary arithmetic operators Learn about the precedence and ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 34
Provided by: daneggeCo
Category:

less

Transcript and Presenter's Notes

Title: Object-Oriented Programming Using C Third Edition


1
Object-Oriented Programming Using CThird
Edition
  • Chapter 2
  • Evaluating C Expressions

2
Objectives
  • Use C binary arithmetic operators
  • Learn about the precedence and associativity of
    arithmetic operations
  • Examine shortcut arithmetic operators
  • Use other unary operators
  • Evaluate Boolean expressions
  • Perform operations on struct fields

3
Using C Binary Arithmetic Operators
  • Five simple arithmetic operators
  • the addition operator ()
  • the subtraction operator ()
  • the multiplication operator ()
  • the division operator (/)
  • the modulus operator ()

Note that they are all binary operators
4
Using C Binary Arithmetic Operators (continued)
5
Using C Binary Arithmetic Operators (continued)
  • Addition, subtraction, multiplication, division,
    or modulus of any two integers results in an
    integer
  • For example, 7 / 3 evaluates to 3
  • Mixed expression operands have different data
    types
  • For example, 3.2 2
  • Unifying type
  • Data type of the value that occupies more memory
  • All types in the expression are temporarily
    converted to a unifying type

6
Using C Binary Arithmetic Operators (continued)
  • The order of precedence of unifying types from
    highest to lowest
  • long double
  • double
  • float
  • unsigned long
  • long
  • unsigned int
  • int
  • short
  • char

7
Explicit cast
8
(No Transcript)
9
Using C Binary Arithmetic Operators (continued)
  • Cast transform a value to another data type
  • Implicit cast automatic cast, or transformation,
    that occurs when you assign a value of one type
    to a type with higher precedence
  • int answer 2.0 7
  • Explicit cast deliberate cast
  • intResult static_castltintgt(doubleVariable)
  • intResult (int)doubleVariable
  • static_castltintgt('A') // is 65

Preferred method in C
10
Using Modulus
  • Modulus () gives the remainder of integer
    division
  • 7 3 results in 1
  • -10 8 produces -2
  • -10 -8 produces -2
  • Can be used only with integers
  • Can be used to extract digits from numbers
  • 6,543 10 is 3
  • 6,789 10 is 9

11
Using Modulus (continued)
  • Check digit digit added to a number that
    validates the authenticity of the number
  • A simple algorithm
  • Assume an account number named acctNum is 123454
  • Remove the last digit
  • Perform modulus on the new number with an
    arbitrary value, say 7 (remainder shortAcctNum
    7)
  • Compare the last digit of the original account
    number (lastDigit acctNum 10) with the
    remainder from the check digit calculation

12
Precedence and Associativity of Arithmetic
Operators
  • Multiplication, division, and modulus are said to
    have higher arithmetic precedencethat is, they
    are performed first in an arithmetic statement
    with multiple operations
  • Associativity rule that dictates the order in
    which an operator works with its operands
  • In C, most operators have left-to-right
    associativity

13
Precedence and Associativity of Arithmetic
Operators (continued)
  • When C evaluates a mixed arithmetic expression,
    the following steps occur
  • The leftmost operation with the highest
    precedence is evaluated
  • If operands are the same type, the result is the
    same data type
  • If the operands are different types, C performs
    an implicit cast and the result is the same type
    as the one that occupies more memory
  • Each subsequent , /, or operation is evaluated
    in the same manner from left to right

14
Precedence and Associativity of Arithmetic
Operators (continued)
  • Steps (continued)
  • The leftmost operation with the lower precedence
    ( and ) is evaluated
  • If operands are the same type, the result is the
    same type
  • If operands are different types, C performs an
    implicit cast and the result is the same type as
    the one that occupies more memory
  • Each subsequent or operation is evaluated in
    the same manner from left to right
  • Use parentheses to override precedence rules

15
Precedence and Associativity of Arithmetic
Operators (continued)
16
Shortcut Arithmetic Operators
  • Two categories of shortcut arithmetic operators
    are
  • Compound assignment operators
  • Increment and decrement operators

17
Compound Assignment Operators
  • Add and assign operator ()
  • Subtract and assign operator ( )
  • Multiply and assign operator ()
  • Divide and assign operator (/)
  • Modulus and assign operator ()

18
Increment and Decrement Operators
  • Prefix increment operator
  • count
  • Postfix increment operator
  • count
  • Prefix decrement operator
  • --count
  • Postfix decrement operator
  • count--

19
Using Shortcut Arithmetic Operators
20
Other Unary Operators
  • Positive value operator
  • 5
  • Negative value operator
  • -8
  • Address operator ()
  • x

21
Other Unary Operators
Hexadecimal number
22
Evaluating Boolean Expressions
  • Relational operators evaluate the relationship
    between operands
  • Used to evaluate Boolean expressions
  • Boolean expression interpreted as true or false

23
Evaluating Boolean Expressions (continued)
  • The unary operator ! is the not operator
    reverses the true/false value of an expression
  • coutltlt(9gt2) displays a 1
  • coutltlt!(9gt2) displays a 0
  • !0 is 1
  • !1 is 0
  • !5 is 0
  • !-5 is 0
  • Dont confuse with

24
Performing Operations on struct Fields
25
Performing Operations on struct Fields (continued)
26
You Do It Using Arithmetic Operators
  • int a,b,c
  • double x,y,z
  • a 13
  • b 4
  • x 3.3
  • y 15.78
  • c a b
  • coutltlta b is ltltcltltendl
  • z x y
  • cout ltltx y is ltltzltltendl
  • c a / b
  • coutltlta / b is ltltcltltendl
  • c a b
  • coutltlta b is ltltcltltendl

27
You Do It Using Arithmetic Operators (continued)
28
Using Prefix and Postfix Increment and Decrement
Operators
  • a 2
  • c a
  • coutltlta is ltltaltlt and c is ltltcltltendl
  • a 2
  • c a
  • coutltlta is ltltaltlt and c is ltltcltltendl

29
Using Prefix and Postfix Increment and Decrement
Operators (continued)
30
Using Operators with struct Fields
  • coutltltPlease enter a student's credit hours
  • cingtgtoneSophomore.creditHours
  • coutltltPlease enter the student's grade point
    average
  • cingtgtoneSophomore.gradePointAverage
  • coutltltThe number of credit hours is ltlt
  • oneSophomore.creditHoursltltendl
  • coutltltThe grade point average is ltlt
  • oneSophomore.gradePointAverageltltendl
  • hoursRemaining HOURS_REQUIRED_TO_GRADUATE
    oneSophomore.creditHours
  • coutltltThis student needs ltlthoursRemainingltlt
  • more credit hours to graduateltltendl

31
Using Operators with struct Fields (continued)
32
Summary
  • There are five simple binary arithmetic
    operators addition (), subtraction (),
    multiplication (), division (/), and modulus ()
  • When you mix data types in a binary arithmetic
    expression, the result is always the same type as
    the type that takes the most memory to store
  • Several shortcut operators for arithmetic exist,
    such as , prefix , and postfix

33
Summary (continued)
  • Boolean expression evaluates as true or false
  • In C, the value 0 is always interpreted as
    false all other values are interpreted as true
  • Fields contained within structures are used in
    arithmetic and Boolean expressions in the same
    manner as are primitive variables
Write a Comment
User Comments (0)
About PowerShow.com