[CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CS320 Course Materials] >> [Sebesta] >> [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Wed Jan 7 15:08:59 PST 2004
```. CS320 Expressions and Assignments
http://www.csci.csusb.edu/cs320/sebesta/07.html

7.1 Introduction

7.2 Arithmetic Expressions
Operators are unary, binary, ternary, ... n-ary
With 1, 2, 3, ...., n operands.
n is sometimes called the 'arity'[sic]

7.2.1 Order of evaluation
7.2.1.1 Precedence
Suppose our language has operators @ and #
What does
a @ b # c mean?
(a @ b) # c
a @ (b # c)

All similar but for the details.

Prolog ?
Operator precedences are part of the operator's declaration.
You can choose your own precedence and assoiativity!
Parsed into a tree.
But not implicitly evaluated
-- you'll meet this in a lab later

LISP?????
LISP has no infix operators so the problem does not arise!
The whole purpose of it's "Polish" notation
was to eliminate the need for precedence rules.
In LISP it can not happen since all expressions are
fully parenthesized.

7.2.1.2 Associativity
When two operators have equal precedence.
a @ b @ c ?
(a @ b) @ c
or
a @ (b @ c)
Why?

7.2.1.3	Parentheses
Make precedence and associativity explicit.
Overrides normal precedence and associativity.

7.2.1.4	Conditional Expressions
Easy when you figure them out.  It is possible to treat them
just like normal mathematical expressions!  Mathematicians
have even invented functions that can be used to get this
effect: Heaviside, Kroniecker.  For example, here is
a function
y= (x <= 0? x*x*x : x*x).
You can draw its graph... and differentiate it:
dy/dx=( x <= 0? 3*x*x: 2*x).
(however, in general, (_?_:_) gives discontinuities)

A quick history of conditional expressions:
Yes		No
195*	LISP
1960	Algol 60	COBOL, FORTRAN
1968	Algol 68
197?	CPL		Pascal
197?	B
197?	C		Modula
1998	C++,Java,JavaScript

7.2.2 Operand Evaluation Order
Expr1 + Expr2 ---- which is evaluated FIRST?
Not always predictable
7.2.2.1	Side effects
Hard to avoid -- Ada specification states that functions with
side effects are not valid programs but no compiler
checks for them.

C does not let you defined/overload existing operators.

C++ has overloaded functions and operators.
C++ allows programmers to overload functions and operators.
Also over-riding and polymorphism.

a unique primitive function or Lambda expression.

Prolog may have overloaded arithmetic in some contexts
operators and functions to this arithmetic.
You have to start afresh and define your own arithmetic.
You can and do write predicates that use the type
of the arguments to determine their behavior.

Java has overloaded operators and functions.
Java allows the programmer to overload functions
Also over-riding and polymorphism.
but you can not declare operators.

Also see Polymorphism in Object Oriented and functional languages.

7.4 Type conversion
Coercion and Mixed Mode Expressions

In C++ you can define coercions and casts.
In C++ there three or four kinds of casts: static, dynamic, etc etc.

Implicit
PL/1, APL, C, ..., C++
It can get tricky.
Explicit:
It gets boring.
Neither
LISP, Prolog

In Ada95 data types are organized into hierarchies
(like all the integer data types)
and implicit conversion within a hierarchy is ok.

to do unchecked conversion between types.  A neat idea:
Let the programmer have freedom, in return for documenting
the risky code.

7.5 Relational and Boolean Expressions

Notice that the pre-90 FORTRANs used .EQ. etc not ==

Relational
C/C++/Java
<	>	<=	>=
==
!=

LISP:  eq, equal, equ, = <=	>=	<	>

Prolog:	=	(unification fails if expressions can not be equal)
is	(can assign a value to unused variable or else it
tests the old value but can not change it)
=:=	=\=	<	>	=<	>=
(comparison of values)
Boolean
C	 	&&	||	!	1	0
C++/Java	&&	||	!	true	false
C++     	and	or	not	true	false
LISP:		and	or	not	T	NIL
Smalltalk: 	and	or	not	True	False
UNIX Shell:	&&	||  	 	(fail=false, other=true!)
Prolog:		,	;	not	true	fails*
Prolog is abnormal, 'not' can destroy information.

7.6 Short Circuit Evaluation
C/C++/Java (YES!)
Pascal (No)
and	vs 	and then
or	vs	or else
LISP?
Prolog?
Smalltalk?

Exercise: Prove that you can express boolean operations by using
conditional statements and/or expressions.

7.7 Assignment
Simple
Multiple targets
Conditional targets!
Compound assignments
Unary Assignment

Assignment as returning a result!
Experiments on students indicate this is not a wise idea.

Notice:
Logic Programming avoids assignments(prolog)
Function programming avoids assignments(LISP)
Objects:  C++, Ada, Java all have means of
defining objects
that can be assigned,
that can not be assigned,
that can be copied, moved, or cloned,
that can assigned and copied in a special way
that may be sometimes assigned.

7.8 Mixed-mode assignment.

MISSING.   C bit-wise operators: & ^ | ~
work on every bit in the biary code.
&	1 if both bits are 1
|	1 if either bit is 1
^	1 if the bits are different!
~	1 if 0 and vice versa.

Exercise:
On the WWW try looking up 'expression' using
http://www.csci.csusb.edu/dick/cs320/lookup.php?search=expression
NOTE ANY SURPRISES.

Notes on the Revision exercises and Research projects.

Here is a translation of the Pascal in number 14 into C++.
int FUN(int & K)
{	K=K+4; return 3*K-1; }

....
I=10;
SUM1=(I/2.0)+FUN(I);
J=10;
SUM2=FUN(J)+(J/2.0);

Compare 14 with 15.

Next
http://www.csci.csusb.edu/cs320/sebesta/08.html