Chap 07
Chap 07
Chapter 7 Topics
Introduction
Arithmetic Expressions
Overloaded Operators
Type Conversions
Relational and Boolean Expressions
Short-Circuit Evaluation
Assignment Statements
Mixed-Mode Assignment
Chapter 7
Expressions and Assignment Statements
Introduction
Expressions are the fundamental means of specifying computations in a
programming language.
To understand expression evaluation, need to be familiar with the orders of
operator and operand evaluation.
Essence of imperative languages is dominant role of assignment statements.
Arithmetic Expressions
Their evaluation was one of the motivations for the development of the first
programming languages.
Most of the characteristics of arithmetic expressions in programming
languages were inherited from conventions that had evolved in math.
Arithmetic expressions consist of operators, operands, parentheses, and
function calls.
The operators can be unary, or binary. C-based languages include a
ternary operator, which has three operands (conditional expression).
The purpose of an arithmetic expression is to specify an arithmetic
computation.
An implementation of such a computation must cause two actions:
o Fetching the operands from memory
o Executing the arithmetic operations on those operands.
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?
A + (- B) * C // is legal
A + - B * C // is illegal
2. Associativity
The operator associativity rules for expression evaluation define the order
in which adjacent operators with the same precedence level are
evaluated. An operator can be either left or right associative.
Typical associativity rules:
o Left to right, except **, which is right to left
o Sometimes unary operators associate right to left (e.g., FORTRAN)
Ex: (Java)
a – b + c // left to right
Ex: (Fortran)
A ** B ** C // right to left
APL is different; all operators have equal precedence and all operators
associate right to left.
Ex:
A X B + C // A = 3, B = 4, C = 5 Î 27
(A + B) * C
4. Conditional Expressions
Sometimes if-then-else statements are used to perform a conditional
expression assignment.
if (count == 0)
average = 0;
else
average = sum / count;
Ex:
• Side Effects
A side effect of a function, called a functional side effect, occurs when
the function changes either one of its parameters or a global variable.
Ex:
a + fun(a)
If fun does not have the side effect of changing a, then the order of
evaluation of the two operands, a and fun(a), has no effect on the
value of the expression.
However, if fun changes a, there is an effect.
Ex:
Consider the following situation: fun returns the value of its argument
divided by 2 and changes its parameter to have the value 20, and:
a = 10;
b = a + fun(a);
int a = 5;
int fun1() {
a = 17;
return 3;
}
void fun2() {
a = a + fun1(); // C language a = 20; Java a = 8
}
void main() {
fun2();
}
Coercion in Expressions
• A mixed-mode expression is one that has operands of different types.
• A coercion is an implicit type conversion.
• The disadvantage of coercions:
They decrease in the type error detection ability of the compiler
• In most languages, all numeric types are coerced in expressions, using
widening conversions
• Language are not in agreement on the issue of coercions in arithmetic
expressions.
• Those against a broad range of coercions are concerned with the
reliability problems that can result from such coercions, because they
eliminate the benefits of type checking.
• Those who would rather include a wide range of coercions are more
concerned with the loss in flexibility that results from restrictions.
• The issue is whether programmers should be concerned with this category
of errors or whether the compiler should detect them.
• Java method Ex:
void mymethod() {
int a, b, c;
float d;
…
a = b * d;
…
}
Java:
Errors in Expressions
• Caused by:
– Inherent limitations of arithmetic e.g. division by zero
– Limitations of computer arithmetic e.g. overflow or underflow
• Floating-point overflow and underflow, and division by zero are examples
of run-time errors, which are sometimes called exceptions.
Relational and Boolean Expressions
• A relational operator: an operator that compares the values of its tow
operands.
• Relational Expressions: two operands and one relational operator.
• The value of a relational expression is Boolean, unless it is not a type
included in the language.
– Use relational operators and operands of various types.
– Operator symbols used vary somewhat among languages (!=, /=,
.NE., <>, #)
• The syntax of the relational operators available in some common
languages is as follows:
C-Based
Operation Ada Fortran 95
Languages
Equal = == .EQ. or ==
Not Equal /= != .NE. or <>
Greater than > > .GT. or >
Less than < < .LT. or <
Greater than or equal >= >= .GE. or >=
Less than or equal <= <= .LE. or >=
Boolean Expressions
• Operands are Boolean and the result is Boolean.
• Versions of C prior to C99 have no Boolean type; it uses int type with 0 for
false and nonzero for true.
• One odd characteristic of C’s expressions:
a < b < c is a legal expression, but the result is not what you might
expect.
• The left most operator is evaluated first because the relational operators of
C, are left associative, producing either 0 or 1.
• Then this result is compared with var c. There is never a comparison
between b and c.
Short Circuit Evaluation
So when a < 0, there is no need to evaluate b, the constant 10, the second
relational expression, or the && operation.
Unlike the case of arithmetic expressions, this shortcut can be easily
discovered during execution.
Short-circuit evaluation exposes the potential problem of side effects in
expressions
Conditional Targets
Ex:
a = a + b
Assignment as an Expression
This design treats the assignment operator much like any other binary
operator, except that it has the side effect of changing its left operand.
Ex:
a = b + (c = d / b++) – 1
if (x = y) …
instead of
if (x == y) …
Mixed-Mode Assignment
In FORTRAN, C, and C++, any numeric value can be assigned to any
numeric scalar variable; whatever conversion is necessary is done.
In Pascal, integers can be assigned to reals, but reals cannot be assigned to
integers (the programmer must specify whether the conversion from real to
integer is truncated or rounded.)
In Java, only widening assignment coercions are done.
In Ada, there is no assignment coercion.
In all languages that allow mixed-mode assignment, the coercion takes place
only after the right side expression has been evaluated. For example,
consider the following code:
int a, b;
float c;
…
c = a / b;
Because c is float, the values of a and b could be coerced to float before the
division, which could produce a different value for c than if the coercion were
delayed (for example, if a were 2 and b were 3).