1 / 19

Expressions and assignment

Learn about arithmetic expressions, operator precedence, associativity, conditional expressions, overloading, coercion, logical expressions, and assignment in programming languages.

gbrinson
Télécharger la présentation

Expressions and assignment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Expressions and assignment • Points for discussion • Arithmetic expressions • Overloading • Logical expressions • Assignment

  2. 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.)

  3. 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)

  4. Precedence rules Precedence rules differ a lot between languages. • Pascal • multiplicative > additive • C • self-increment and unary >multiplicative > additive • Ada • ** > multiplicative >unary + and - > additive • Fortran • ** > multiplicative > additive

  5. 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.

  6. 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 + zcorrectly means (x * y) + z • x + y * z means (x + y) * z(!)

  7. Conditional expressions • First introduced in Algol 60: • if x > 0 then 1 elseif 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!

  8. 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

  9. 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++.

  10. , , b c d 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. • conjunction a :- b, c, d. • argument separator a(b, c, d) • list element separator [b, c, d] • a functor: • (b, c, d) means

  11. 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.

  12. 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

  13. Logical operators • Pascal not,and,or • Java !,&&,&,||,| • Ada not,and,andthen, or,orelse,xor • Short-circuit operations perform lazy evaluation: they stop after the first true in "or", after the first false in "and". The operators &&,andthen,||,orelse all work like this.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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;

More Related