Expressions and assignment
W
Shared by: wql24865
Categories
Tags
assignment operator, arithmetic operators, assignment statements, boolean expression, arithmetic expressions, assignment statement, data types, logical operators, assignment operators, unary operators, boolean expressions, relational operators, array assignment, operator precedence, pointer assignment
-
Stats
- views:
- 18
- posted:
- 2/23/2010
- language:
- English
- pages:
- 19
Document Sample


Expressions and assignment
Points for discussion
• Arithmetic expressions
• Overloading
• Logical expressions
• Assignment
CSI 3125, Expressions and assignment, page 1
Arithmetic expressions
An arithmetic expression is a function that
maps one or more numbers into a number.
Operators are usually written in the infix form
(unless it is an expression in Scheme J).
Operator precedence or strength: the order of
evaluation of expressions with more than one
operator. (Parentheses can be used to specify
order explicitly.)
CSI 3125, Expressions and assignment, page 2
Arithmetic expressions (2)
Operators are usually grouped as follows.
exponentiation: **
unary operators: abs, prefix + and -, etc.
(negation is also a unary operator)
multiplicative: *, /, div, mod etc.
(conjunction is also multiplicative)
additive: binary +, binary - etc.
(disjunction is also additive)
CSI 3125, Expressions and assignment, page 3
Precedence rules
Precedence
rules differ a
lot between Pascal
languages. multiplicative > additive
C
self-increment and unary >
multiplicative > additive
Ada
** > multiplicative >
unary + and - > additive
Fortran
** > multiplicative > additive
CSI 3125, Expressions and assignment, page 4
Associativity (1)
Let • be any binary operator. It associates:
left to right if x • y • z = (x • y) • z
Pascal, Ada, C (all normal operators)
right to left if x • y • z = x • (y • z)
C (self-increment operators ++ and --)
There also are nonassociative operators, such as
exponentiation in Ada:
x ** y ** z is syntactically incorrect, though
(x ** y) ** z and x ** (y ** z) are OK.
CSI 3125, Expressions and assignment, page 5
Associativity (2)
Some language have no precedence and one
associativity rule.
In APL: always right to left.
x + y * z correctly means x + (y * z)
x * y + z means x * (y + z) (!)
In Smalltalk: always left to right.
x * y + z correctly means (x * y) + z
x + y * z means (x + y) * z (!)
CSI 3125, Expressions and assignment, page 6
Conditional expressions
First introduced in Algol 60:
if x > 0 then 1 else
if x = 0 then 0 else -1
The same is available in the C family:
(x > 0 ? 1 : (x == 0 ? 0 : -1))
Conditional expressions are essential in
Scheme—they are a principal control
structure!
CSI 3125, Expressions and assignment, page 7
Overloading
Overloading occurs when a name or symbol has
several different uses or meanings. Here are a
few examples in Pascal:
+ integer addition, floating-point addition, string
concatenation, set union
* integer multiplication, floating-point
multiplication, set intersection
abs integer integer, real real
CSI 3125, Expressions and assignment, page 8
Overloading (2)
Overloading can be always resolved by context
if all operands have known types.
In 2 + 3 the plus works with integers,
in "a" + "cde" there are strings.
In Ada, overloading is an important element of
the language design. Ada is extendible: a new
meaning can be given to an operator in addition
to the present meaning.
Overloading is also possible in C++.
CSI 3125, Expressions and assignment, page 9
Overloading (3)
Overloading can be quite confusing. In C:
& bitwise conjunction and address,
* multiplication and dereferencing.
In PL/I, = denotes both equality and assignment.
In Prolog, the comma is heavily overloaded.
1. conjunction a :- b, c, d.
2. argument separator a(b, c, d)
3. list element separator [b, c, d]
4. a functor: ,
(b, c, d) means b ,
c d
CSI 3125, Expressions and assignment, page 10
Coercion
If objects of two numeric types appear as
operands, we "upgrade" the lower type.
Numeric type hierarchy in Fortran:
integer < real < double < complex
In Java byte, short and char are almost
always coerced to int before an aritmetic
operation is applied—and converted back from
int afterwards.
CSI 3125, Expressions and assignment, page 11
Logical expressions
The comparison operators have many forms
equal = == .EQ.
not equal <> != .NE.
less < < .LT.
less or equal <= =< .LE.
greater > > .GT.
greater or equal >= => .GE.
Pascal Fortran IV
CSI 3125, Expressions and assignment, page 12
Logical operators
Pascal not, and, or
Java !, &&, &, ||, |
Ada not, and, and then,
or, or else, xor
Short-circuit operations perform lazy evaluation:
they stop after the first true in "or", after the first
false in "and". The operators &&, and then, ||,
or else all work like this.
CSI 3125, Expressions and assignment, page 13
Assignment
Assignment works in the same way in all
imperative and object-oriented languages. Only
the operators may look differently:
target := expression Algol, Pascal, Ada
target = expression Fortran, C, Java
target expression Smalltalk
There is no assignment in Prolog, and nothing
like it in pure Scheme.
CSI 3125, Expressions and assignment, page 14
Multiple assignment
Multiple assignment is more interesting.
PL/I: A, B := EXPR;
This is quite obvious: calculate the value of
EXPR and send to A and to B, in any order.
Algol 60: A := B := EXPR;
(Step 1) Find the value of EXPR.
(Step 2) Assign this value to B, then to A.
(or Step 2) Assign this value to A, then to B.
CSI 3125, Expressions and assignment, page 15
Multiple assignment (2)
The order may be important. Consider:
I := 5; A[I] := I := 10;
The order in which target addresses are
calculated may change the result.
One method:
(1) Find all target addresses.
(2) Find the value of EXPR.
(3) Assign this value to A and B.
With this method, A[5] := 10.
CSI 3125, Expressions and assignment, page 16
Multiple assignment (3)
Another method:
(1) Find the value of EXPR.
(2) Find target addresses left-to-right,
assign the value to every address.
With this method, again A[5] := 10.
A third method:
(1) Find the value of EXPR.
(2) Find target addresses right-to-left,
assign the value to every address.
With this method, A[10] := 10.
CSI 3125, Expressions and assignment, page 17
More on assignment
This statement in C is not a multiple
assignment:
A = B = EXPR;
Here, B = EXPR has a value (equal to
the value of EXPR). This value is next
assigned to A: the assignment operator
in C associates right-to-left.
CSI 3125, Expressions and assignment, page 18
More on assignment (2)
Another well-known syntactic variation in C,
C++, Java mixes assignment with arithmetics.
A += B; means A = A + B;
A *= B; means A = A * B;
and so on.
Finally, in C++ we can have conditional
targets. This may be really hard to follow.
(x != 0 ? y : z) = 17;
or (even less readable!)
x ? y : z = 17;
CSI 3125, Expressions and assignment, page 19
Related docs
Get documents about "