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

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
COEN Expressions and Assignment
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CS2403 Programming Languages Expressions and Assignment Statements Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are.
CSE 452: Programming Languages Expressions and Control Flow.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
1 Chapter 7 Expressions and Assignment statements.
C HAPTER 7 Expressions and Assignment Statements.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright Curt Hill Casts What are they? Why do we need them?
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 CS Programming Languages Class 10 September 26, 2000.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
More important details More fun Part 3
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
What are they? Why do we need them?
Expressions and Assignment Statements
Expressions & Assignments
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Expressions and Assignment Statements
Expression and Asignment Statements
Chapter 7 Expressions and Assignment Statements.
Chapter 7: Expressions and Assignment Statements Sangho Ha
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

Copyright © by Curt Hill Expressions and Assignment Statements Part 2

Copyright © 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

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill

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 © by Curt Hill