Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003-2014 by Curt Hill Expressions and Assignment Statements Part 2.

Similar presentations


Presentation on theme: "Copyright © 2003-2014 by Curt Hill Expressions and Assignment Statements Part 2."— Presentation transcript:

1 Copyright © 2003-2014 by Curt Hill Expressions and Assignment Statements Part 2

2 Copyright © 2003-2014 by Curt Hill Type Conversions Conversions come in two flavors: Widening –Conversion to a stronger type –Such as float to double Narrowing –Conversion to a weaker type –Such as double to float Called casts in C family

3 Safety Widening is usually safe, but narrowing loses information However, even some widening can lose information if the basic type is changed –int and float are the same size in C –A very large value would lose precision Copyright © 2003-2014 by Curt Hill

4 Coercions A mixed mode expression has multiple types A coercion is a conversion generated by compiler –Sometimes called an automatic cast Coercions can be detrimental to reliability and they eliminate the opportunities for type checking With strong type checking only the programmer does conversions, so they always know when they happen Copyright © 2003-2014 by Curt Hill

5 More Most languages do allow coercions, they tend to hide certain kinds of mistakes PL/I got carried away: –Suppose an arithmetic operator between a string and a numeric variable –Check if string contained a number –If it did it was converted –If it contained a decimal point or E it became a real otherwise an integer –The other variable might then be converted from integer to real Copyright © 2003-2014 by Curt Hill

6 Explicit conversions There is usually a syntax for these conversions Ada and Modula-2 make conversions look like function calls C and Java parenthesize the type instead of the argument, because of multi word types C++ does both Pascal has a number of conversion functions Copyright © 2003-2014 by Curt Hill

7 Relational expressions Comparisons between values of similar type that produce a boolean –If language has no boolean then the closest equivalent –C used almost any numeric type as Booleans: zero is false anything else true –Even a pointer was a type of integer These are overloaded for all comparable types Copyright © 2003-2014 by Curt Hill

8 Relational Operators FORTRAN has non-symbols:.eq..gt..lt..ne.ge..le. –Because was not a keypunch character then –Needed dots to separate from operands Most others use –Except equality may be = or == and inequality may be <> /= or != PhP and JavaScript have === and !== –No coercion Copyright © 2003-2014 by Curt Hill

9 Boolean expressions There are certain common boolean operators: AND, OR, NOT as well as some less common like XOR These are not relationals –Must take one or two booleans and return a boolean The problem is precedence –Boolean operators take Booleans –relational produce booleans but take numerics and some others –Arithmetics take numerics and produce numerics Copyright © 2003-2014 by Curt Hill

10 Precedence Precedence has to take into account that an expression may include boolean, relational and arithmetic operators Typically NOT has high, AND, OR low precedence C is odd since there is no boolean: –a < b < c is legal but does not do what is desired Pascal: AND and OR have precedence comparable to multiply and add, which is higher than Copyright © 2003-2014 by Curt Hill

11 What do we do with this? (a<>0) AND (func(x)/a>5) If a is zero the second item has a divide by zero exception The function call may have a side effect Two answers: –Always evaluate – we get the side effect –Short circuit evaluation – no divide by zero Copyright © 2003-2014 by Curt Hill

12 Short Circuit Evaluation Short circuit evaluation is the determination of a final value without fully evaluating the expression In arithmetic a multiplication by zero can be shortened, but more commonly boolean evaluation is eligible FALSE AND X is always false; TRUE OR Y is always true regardless of the the values of x and y Copyright © 2003-2014 by Curt Hill

13 Issues Short circuit evaluation is good: (a<>0) AND (b/a>5) –Prevents divide by zero Short circuit evaluation is bad in cases where a function with side effects is not executed and the side effects are needed –Many languages allow the implementer to decide –Pascal forbids short circuit evaluation, but many compilers make it a compile option Copyright © 2003-2014 by Curt Hill

14 More Since AND is associative you cannot just put the function call first since the compiler may optimize the order Ada is the only one with explicit control: “and” is long, “and then” is short –if x 2 uses short circuit evaluation –if x 2 does both Copyright © 2003-2014 by Curt Hill

15 Assignments Statements The assignment statement is the most common statement in imperative languages It is also the main consumer of expressions in most languages Issues –Simple vs. multiple –Operator or statement Copyright © 2003-2014 by Curt Hill

16 The assignment statement An imperative language without an assignment statement is hard to imagine It is the defining characteristic of the imperative paradigm A non-imperative language may do quite nicely without an assignment –Consider the dominance of imperative languages over non-imperative languages Copyright © 2003-2014 by Curt Hill

17 Purpose The notion of an assignment is to place a new value in a variable This may be a simple copy but more likely it involves some computation on arithmetic, logical or other operands This is a simple abstraction of the model of memory Copyright © 2003-2014 by Curt Hill

18 Simple assignments The operator is usually = or := –APL uses a  operator The choice of operator needs to be clearly distinct from the equality operator –PLI and BASIC use = for both It may be its own statement or it may be just an operator in an expression Copyright © 2003-2014 by Curt Hill

19 Multiple assignment Multiple assignments are nice but optional –They are not that commonly used in programs PLI uses a comma for multiple assignment: a,b=c –a=b=c is the assignment of a boolean to a in PLI, but a multiple assignment in C C uses a different syntax since = is an operator Copyright © 2003-2014 by Curt Hill

20 C Family This operator allows a very concise use of it which is difficult for other languages: –while(a=b/2) –Uses the value of the b/2 to drive the while and change a at the same time Copyright © 2003-2014 by Curt Hill

21 Other Multiples Perl, Lua and Ruby allow: –($a, $b, $c) = (1,2,3); –Same as: $a=1; $b=2;$c=3 Also remember the Move Corresponding from COBOL Copyright © 2003-2014 by Curt Hill

22 Compound assigns C family also loves the compound assignments: –a+=b*c –Introduced by ALGOL This family also has the unary assignments The ++ and -- are actually special assignment operators Copyright © 2003-2014 by Curt Hill

23 Assignments as expressions C also allows: a = (b=c*f)+(d=e); The advantage is a very concise notation –The above in Pascal is: b:=c*f; d:=e; a:=b + d; The disadvantage is the famous error of confusing the assignment for the equivalence: if(a=b) instead of if(a==b) Copyright © 2003-2014 by Curt Hill

24 Conditional Targets C family has a conditional expression –X = a>b?5:x-1; Perl has a conditional target: –($flag ? $total : $subtotal) = 0 –Which is equivalent to –if ($flag){ $total = 0 } else { $subtotal = 0 } Copyright © 2003-2014 by Curt Hill

25 Mixed-Mode Assignment The main problem is the coercions Does the expression on the RHS get coerced into the LHS type or not? –C, C++, FORTRAN vote yes in any case where a suitable coercion is available –Pascal says yes only for converting integers to reals –Ada and Modula2 disallow –Java allows if the coercion is a widening Copyright © 2003-2014 by Curt Hill

26 Another The book gives a proposal that is unimplemented Each operand on RHS be coerced into the type of the RHS This would tend to alleviate some problems and add new ones Copyright © 2003-2014 by Curt Hill


Download ppt "Copyright © 2003-2014 by Curt Hill Expressions and Assignment Statements Part 2."

Similar presentations


Ads by Google