[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 

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 Precedence
	Suppose our language has operators @ and #
	What does
		a @ b # c mean?
		(a @ b) # c
		a @ (b # c)

	Ada, Pascal. ANSI C, FORTRAN
		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 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. Associativity
	When two operators have equal precedence.
		a @ b @ c ?
			(a @ b) @ c
			a @ (b @ c)
	Why?	Parentheses
	Make precedence and associativity explicit.
	Overrides normal precedence and associativity.	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
	1983			Ada  83 (NO!)
	1995	Ada  95 (yes)
	1998	C++,Java,JavaScript

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

7.3   Overloaded Operators
	C did not allow overloaded functions but had overloaded operators.
	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.

	Pascal had predefined overloaded operators but did not
		allow usedefined overloadings.

	LISP does not permit overloading.  The function name identifies
		a unique primitive function or Lambda expression.

	Prolog may have overloaded arithmetic in some contexts
		but you can not add
		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.

	Ada has overloaded functions, procedures, and operators.
	Ada allows programmer to overload functions, procedures, and operators.

	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.

			PL/1, APL, C, ..., C++
			It can get tricky.
			Ada83, Ada95*, Java, smalltalk
			It gets boring.
			LISP, Prolog

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

	In Ada83, you could explicitly ask for a function
		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 ==

		<	>	<=	>=

	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)
	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)
	Ada (BOTH!)
		and	vs 	and then
		or	vs	or else

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

7.7 Assignment
	Multiple targets
	Conditional targets!
	Compound assignments
	Unary Assignment

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

		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.

	On the WWW try looking up 'expression' using

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; }


Compare 14 with 15.

   See also