Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Expressions and
Assignment Statements
ISBN 0-321-33025-0
Chapter 7 Topics
Introduction
Arithmetic Expressions
Overloaded Operators
Type Conversions
Relational and Boolean Expressions
Short-Circuit Evaluation
Assignment Statements
Mixed-Mode Assignment
Copyright 2006 Addison-Wesley. All rights reserved.
1-2
Introduction
Arithmetic evaluation was one of the
motivations for the development of the first
programming languages
Arithmetic expressions consist of operators,
operands, parentheses, and function calls
15 * (a + b) / log(x)
1-3
1-4
1-5
parentheses
unary operators
** (if the language supports it)
*, /
+, Copyright 2006 Addison-Wesley. All rights reserved.
1-6
1-7
Conditional Expressions
Conditional Expressions
1-8
1-9
a = 10;
b = a + fun(&a); /* assume that fun changes
its parameter */
1-10
1-11
1-12
Referential Transparency
A program has the property of referential
transparency if
1-13
Overloaded Operators
Use of an operator for more than one
purpose is called operator overloading
Some are common (e.g., + for int and
float)
Some are potential trouble (e.g., & in C and
C++)
Keying errors (leaving out the first operand for
&) can go undetected by the compiler
Detrimental to readability
Can be avoided by introduction of new symbols
(e.g., Pascal
s div for integer division)
1-14
1-15
Type Conversions
narrowing conversion
converts an object to a type that cannot
include all of the values of the original
type
e.g., float to int
widening conversion
an object is converted to a type that can
include at least approximations to all of
the values of the original type
e.g., int to float
Copyright 2006 Addison-Wesley. All rights reserved.
1-16
1-17
1-18
Errors in Expressions
Causes
Coercions of operands in expressions
Inherent limitations of arithmetic
e.g., division by zero
Limitations of computer arithmetic
e.g. overflow or underflow
Division by zero, overflow, and underflow
are run-time errors (sometimes called
exceptions)
Copyright 2006 Addison-Wesley. All rights reserved.
1-19
1-20
10
FORTRAN 90
Ada
.AND.
.OR.
.NOT.
and
or
not
&&
||
!
and
or
not
xor
1-21
1-22
11
1-23
1-24
12
1-25
1-26
13
Assignment Statements
The general syntax
<target_var> <assign_operator> <expression>
1-27
Conditional Targets
Conditional targets (C, C++, and Java)
flag ? count1 : count2 = 0
Which is equivalent to
if (flag)
count1 = 0
else
count2 = 0
Copyright 2006 Addison-Wesley. All rights reserved.
1-28
14
Compound Operators
A shorthand method of specifying a
commonly needed form of assignment
Introduced in ALGOL; adopted by C
Example
a = a + b
is written as
a += b
Copyright 2006 Addison-Wesley. All rights reserved.
1-29
1-30
15
Assignment as an Expression
In C, C++, and Java, the assignment
statement produces a result and can be
used as operands (= is treated as an
operator)
An example:
while ((ch = getchar())!= EOF){}
ch = getchar() is carried out; the result
(assigned to ch) is used as a conditional
value for the while statement
Copyright 2006 Addison-Wesley. All rights reserved.
1-31
b to temp
b+1 to b
d / temp to c
b + c to temp
temp 1 to a
1-32
16
1-33
Mixed-Mode Assignment
Assignment statements can also be
mixed-mode, for example
int a = 2, b = 3;
float c;
c = a / b;
1-34
17
Summary
Expressions
Operator precedence and associativity
Operator overloading
Mixed-type expressions
Various forms of assignment
1-35
18