Chapter 7 Expressions and Assignment Statements - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chapter 7 Expressions and Assignment Statements

Description:

Expressions are the fundamental means of specifying computations in a programming language. Arithmetic expressions consist of ... Ternary three operands ... – PowerPoint PPT presentation

Number of Views:920
Avg rating:3.0/5.0
Slides: 27
Provided by: david2548
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Expressions and Assignment Statements


1
Chapter 7Expressions and Assignment Statements
  • CS 350 Programming Language Design
  • Indiana University Purdue University Fort Wayne

2
Chapter 7 Topics
  • Introduction
  • Arithmetic expressions
  • Overloaded operators
  • Type conversions
  • Relational and Boolean expressions
  • Short-circuit evaluation
  • Assignment statements
  • Mixed-mode assignment

3
Introduction
  • Expressions are the fundamental means of
    specifying computations in a programming language
  • Arithmetic expressions consist of operators,
    operands, parentheses, and function calls
  • Arithmetic expressions in imperative languages
    are derived from mathematical notation
  • Consequently, most binary operators are infix
  • The purpose of an assignment statement is to
    change the value of a variable
  • This is how imperative languages change program
    state

4
Arithmetic expressions
  • Design issues for arithmetic expressions
  • 1. What are the operator precedence rules?
  • 2. What are the operator associativity rules?
  • 3. What is the order of operand evaluation?
  • 4. Are there restrictions on operand evaluation
    side effects?
  • 5. Does the language allow user-defined operator
    overloading?
  • 6. What mode mixing is allowed in expressions?

5
Arithmetic expressions
  • To understand expression evaluation, the orders
    of operator and operand evaluation need to be
    understood
  • Operator evaluation order is governed by
  • Precedence rules
  • Associativity rules

6
Precedence rules
  • The operator precedence rules for expression
    evaluation define the order in which adjacent
    operators of different precedence classes are
    evaluated
  • Adjacent means they are separated by at most one
    operand
  • Typical precedence levels
  • 1. parentheses
  • 2. unary operators
  • 3. (if the language supports it)
  • 4. , /
  • 5. , -

7
Associativity rules
  • The operator associativity rules for expression
    evaluation define the order in which adjacent
    operators with the same precedence level are
    evaluated
  • Typical associativity rules
  • Left to right, except , which is right to left
  • APL is different
  • All operators have equal precedence and all
    operators associate right to left
  • Precedence and associativity rules can be
    overridden with parentheses

8
Associativity rules
  • Mathematically, associativity should not make a
    difference in expressions like A B C
  • But computer arithmetic is not always associative
  • Floating-point and integer arithmetic is
    associative only if the result of each operation
    can be exactly represented
  • Example
  • Consider 4-bit twos complement integer
    arithmetic
  • The range of representable values is -8 to 7
  • Suppose a 4, b 4, and c -2
  • a (b c) evaluates to 6
  • (a b) c results in arithmetic overflow

9
Operand evaluation order
  • Operators can be
  • Unary one operand
  • Binary two operands
  • Ternary three operands
  • Operand evaluation order makes a difference if
    operand evaluation causes side effects
  • Side effects are caused by
  • Method calls
  • Unwise use of unary increment or decrement
    operators

10
Operand evaluation order
  • A functional side effect occurs when a non-void
    method changes a two-way parameter or a non-local
    variable
  • Such a side effect interferes with expression
    evaluation when it alters another operand in the
    expression
  • Example
  • Assume non-void method f returns half the value
    of its parameter and doubles the original value
    in the parameter as a side effect
  • The value of the expression at right is . . .
  • 15 if the first operand of is evaluated first
  • 25 if it is evaluated after the second operand
  • A similar problem exists if global variables are
    altered

a 10 b a f(a)
11
Functional side effects
  • There are two possible solutions to the problem
  • Write the language definition to disallow
    functional side effects
  • No two-way parameters in functions
  • No non-local references in functions
  • Advantage it works!
  • Disadvantage The flexibility of two-way
    parameters and non-local references is lost
  • Write the language definition to demand that
    operand evaluation order be fixed
  • Disadvantage It limits some compiler
    optimizations

12
Overloaded operators
  • Operator overloading is the use of an operator
    for more than one purpose
  • Some overloadings are potential trouble
  • For example, the use of in C and C for
    bitwise AND and for the unary address of
    operator
  • Loss of compiler error detection
  • r r1 r2 would compile fine if r1 were omitted
  • Some loss of readability
  • It is better to introduce a new symbol for a
    totally different operation

13
Overloaded operators
  • Some overloadings are common
  • For example, the use of for ints and doubles
  • Problem The C code below does not correctly
    compute the floating-point average of a sum of
    count integers when sum and count are ints
  • Integer division truncates the quotient to an
    int, which is then coerced to floating-point
    (without the fractional part)
  • Pascal avoids this with a special operator, div,
    for integer division

ave sum / count
14
Overloaded operators
  • C and Ada allow user-defined overloaded
    operators
  • Potential problems
  • Users can define nonsense operations
  • Readability may suffer, even when the operators
    make sense
  • Used well, however, a program can be made more
    readable

15
Type conversions
  • A narrowing conversion is one that converts an
    object to a type that cannot include all of the
    values of the original type
  • For example, float to int
  • A widening conversion is one in which an object
    is converted to a type that can include at least
    approximations to all of the values of the
    original type
  • For example, int to float
  • Conversion of large integers results in loss of
    precision
  • Widening conversions are nearly always safe

16
Type conversions
  • A mixed-mode expression is one that has operands
    of different types
  • A corecion is an implicit type conversion
  • Coercions decrease the type error detection
    ability of the compiler
  • Most languages allow mixed-mode expressions in
    which all numeric types are coerced as necessary
    with widening conversions
  • Ada is an exception, with virtually no mixed-mode
    expressions or coercions

17
Explicit type conversions
  • These are often called casts
  • Examples
  • Ada
  • Integer Index is cast to Float using Float(
    Index )
  • Java
  • Double speed is cast to int using (int) speed

18
Relational and Boolean expressions
  • A relational expression
  • Uses relational operators (such as lt and ) and
    operands of various types
  • Evaluates to some Boolean representation
  • Operator symbols used vary among languages
  • Some symbols for not equal
  • ! / ltgt .NE.
  • A Boolean expression has Boolean operands and the
    result is Boolean

FORTRAN 77 FORTRAN 90 C Ada
.AND. and and
.OR. or or .NOT.
not ! not
xor
Some Boolean operators
19
Relational and Boolean expressions
  • Observations concerning C
  • C had no Boolean type prior to C99
  • It uses int type with 0 for false and nonzero for
    true
  • An expression like altb returns 0 or 1
  • C99 and C still accept arithmetic expressions
    in place of Booleans
  • C gives higher precedence to AND than to OR
  • This does not agree with the conventions of
    Boolean algebra
  • In C99, the expression agtbgtc is still legal
  • 11gt7gt5 evaluates to false since 11gt7 evaluates to
    1

20
Short circuit evaluation
  • The Java Boolean operator is a short-circuit
    operator
  • If not for short-circuit evaluation, the
    following code fragment would have a run-time
    error
  • C, C, and Java use short-circuit evaluation for
    the usual Boolean operators ( and )
  • They also provide bitwise Boolean operators that
    are not short circuit ( and )

index 1 while ( index lt length ) ( list
index ! value ) index
21
Short circuit evaluation
  • Ada has both short-circuit and traditional
    Boolean operators
  • Traditional and, or
  • Short-circuit and then, or else
  • Short-circuit evaluation may cause an error when
    the programmer relies on side effects in
    expressions
  • For example (a gt b) ( (b) lt 3 )
  • If agtb, then b is not incremented

22
Assignment statements
  • The operator symbol is typically or
  • in Fortran, Basic, PL/I, C, C, and Java
  • in Algol, Pascal, and Ada
  • If is used, it should not be overloaded for the
    relational operator for equality
  • PL/I does this
  • A B C assigns a Boolean to A

23
Assignment statements
  • Some assignment statement variety
  • Multiple targets
  • PL/I A, B 10
  • C a b c
  • Compound assignment operators
  • C, C, and Java sum increment
  • Unary assignment operators
  • C, C, and Java a
  • Can you tell what effect is produced by
    ?
  • Conditional targets
  • C, C, and Java
  • Can you figure out what this means?

- count
n (a b) ? count index 10
24
Assignment statements
  • C, C, and Java treat as an arithmetic binary
    operator that returns the value assigned to the
    LHS
  • This was inherited from ALGOL 68
  • Useful in
  • Perhaps convenient in
  • Just poor practice in
  • Reliability disadvantage
  • Another kind of expression side effect
  • Readability disadvantage
  • Assignments on the RHS of expressions are easy to
    miss
  • The expression cannot be read as an expression
    denoting a value

while ( ch getchar() ) ! EOF ) ? ? ?
a b (c d 2 1) 1
a b ( c d / b ) 1
25
Assignment statements
  • Problem
  • and are similar, but have completely
    different meanings
  • This is not so bad if the compiler can catch
    mistakes
  • Suppose is inadvertently used for in C or
    C
  • The compiler does not detect the error because .
    . .
  • returns the value of b
  • This value is treated as a Boolean
  • false if b 0
  • true if b is non-zero

if ( a b ) ? ? ?
26
Mixed-mode assignment
  • In FORTRAN, C, and C, any numeric value can be
    assigned to any numeric scalar variable
  • Whatever coercion that is necessary is done
  • In Pascal, integers can be assigned to floats,
    but floats cannot be assigned to integers
  • The programmer must specify whether the
    conversion from real to integer is truncated or
    rounded
  • In Java and C, only widening assignment
    coercions are done
  • In Ada, there is no assignment coercion
Write a Comment
User Comments (0)
About PowerShow.com