Expressions and assignment Points for discussion Arithmetic expressions

  • Slides: 19
Download presentation
Expressions and assignment Points for discussion • Arithmetic expressions • Overloading • Logical expressions

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

Arithmetic expressions An arithmetic expression is a function that maps one or more numbers

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

Arithmetic expressions (2) Operators are usually grouped as follows. exponentiation: ** unary operators: abs,

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

Precedence rules differ a lot between languages. Pascal multiplicative > additive C self-increment and

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 4

Associativity (1) Let • be any binary operator. It associates: left to right if

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

Associativity (2) Some language have no precedence and one associativity rule. In APL: always

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

Conditional expressions First introduced in Algol 60: if x > 0 then 1 else

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

Overloading occurs when a name or symbol has several different uses or meanings. Here

Overloading occurs when a name or symbol has several different uses or meanings. Here a few examples in Pascal: + integer addition, floating-point addition, string concatenation, set union * integer multiplication, floating-point multiplication, set intersection abs integer, real 8

Overloading (2) Overloading can be always resolved by context if all operands have known

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

Overloading (3) Overloading can be quite confusing. In C: & bitwise conjunction and address,

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 , b c d 10

Coercion If objects of two numeric types appear as operands, we "upgrade" the lower

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

Logical expressions The comparison operators have many forms equal = not equal less <

Logical expressions The comparison operators have many forms equal = not equal less < less or equal greater or equal == <> < > . EQ. !=. LT. <= > >= Pascal . NE. =<. LE. . GT. =>. GE. Fortran IV 12

Logical operators Pascal not, and, or Java !, &&, &, ||, | Ada not,

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

Assignment works in the same way in all imperative and object-oriented languages. Only the

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

Multiple assignment is more interesting. PL/I: A, B : = EXPR; This is quite

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

Multiple assignment (2) The order may be important. Consider: I : = 5; A[I]

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

Multiple assignment (3) Another method: (1) Find the value of EXPR. (2) Find target

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

More on assignment This statement in C is not a multiple assignment: A =

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

More on assignment (2) Another well-known syntactic variation in C, C++, Java mixes assignment

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