session 7 (expression and assignment statements)

Introduction ƒ Expressions are the fundamental means of specifying computations in a programming language. ƒ To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation. ƒ Essence of imperative languages is dominant role of assignment statements.
Arithmetic Expressions ƒ Their evaluation was one of the motivations for the development of the first programming languages. ƒ Most of the characteristics of arithmetic expressions in programming languages were inherited from conventions that had evolved in math. ƒ Arithmetic expressions consist of operators, operands, parentheses, and function calls. ƒ The operators can be unary, or binary. C-based languages include a ternary operator, which has three operands (conditional expression). ƒ The purpose of an arithmetic expression is to specify an arithmetic computation. ƒ An implementation of such a computation must cause two actions: o Fetching the operands from memory o Executing the arithmetic operations on those operands. ƒ Design issues for arithmetic expressions: 1. What are the operator precedence rules? 2. What are the operator associativity rules? 3. What is the order of operand evaluation? 4. Are there restrictions on operand evaluation side effects? 5. Does the language allow user-defined operator overloading? 6. What mode mixing is allowed in expressions?
Operator Evaluation Order 1. Precedence ƒ The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated (“adjacent” means they are separated by at most one operand). ƒ Typical precedence levels: 1. parentheses 2. unary operators 3. ** (if the language supports it) 4. *, /
5. +, – ƒ Many languages also include unary versions of addition and subtraction. ƒ Unary addition (+) is called the identity operator because it usually has no associated operation and thus has no effect on its operand. ƒ In Java, unary plus actually does have an effect when its operand is short or byte. An implicit conversion of short and byte operands to int type takes place. ƒ Unary minus operator (-) Ex:

A + (- B) * C // is legal A + – B * C // is illegal

2. Associativity ƒ The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated. An operator can be either left or right associative. ƒ Typical associativity rules: o Left to right, except **, which is right to left o Sometimes unary operators associate right to left (e.g., FORTRAN) ƒ Ex: (Java)

a – b + c // left to right

ƒ Ex: (Fortran)

A ** B ** C // right to left

(A ** B) ** C // In Ada it must be parenthesized

Language Associativity Rule FORTRAN Left: * / + – Right: ** C-BASED LANGUAGES Left: * / % binary + binary – Right: ++ — unary – unary + ADA Left: all except ** Non-associative: **

ƒ APL is different; all operators have equal precedence and all operators associate right to left. ƒ Ex:

A X B + C // A = 3, B = 4, C = 5 Î 27

ƒ Precedence and associativity rules can be overridden with parentheses.

3. Parentheses ƒ Programmers can alter the precedence and associativity rules by placing parentheses in expressions. ƒ A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts. ƒ Ex:

(A + B) * C

4. Conditional Expressions ƒ Sometimes if-then-else statements are used to perform a conditional expression assignment.

if (count == 0) average = 0; else average = sum / count;

ƒ In the C-based languages, this can be specified more conveniently in an assignment statement using a conditional expressions. Note that ? is used in conditional expression as a ternary operator (3 operands).

expression_1 ? expression_2 : expression_3

ƒ Ex:

average = (count == 0) ? 0 : sum / count;

Operand evaluation order ƒ The process: 1. Variables: just fetch the value from memory. 2. Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction. 3. Parenthesized expressions: evaluate all operands and operators first.

• Side Effects ƒ A side effect of a function, called a functional side effect, occurs when the function changes either one of its parameters or a global variable. ƒ Ex:

a + fun(a)

ƒ If fun does not have the side effect of changing a, then the order of evaluation of the two operands, a and fun(a), has no effect on the value of the expression. ƒ However, if fun changes a, there is an effect. ƒ Ex: Consider the following situation: fun returns the value of its argument divided by 2 and changes its parameter to have the value 20, and:

a = 10; b = a + fun(a);

ƒ If the value of a is returned first (in the expression evaluation process), its value is 10 and the value of the expression is 15. ƒ But if the second is evaluated first, then the value of the first operand is 20 and the value of the expression is 25. ƒ The following shows a C program which illustrate the same problem.

int a = 5; int fun1() { a = 17; return 3; } void fun2() { a = a + fun1(); // C language a = 20; Java a = 8 } void main() { fun2(); }

ƒ The value computed for a in fun2 depends on the order of evaluation of the operands in the expression a + fun1(). The value of a will be either 8 or 20. ƒ Two possible solutions: 1. Write the language definition to disallow functional side effects o No two-way parameters in functions. o No non-local references in functions. o Advantage: it works! o Disadvantage: Programmers want the flexibility of two-way parameters (what about C?) and non-local references. 2. Write the language definition to demand that operand evaluation order be fixed o Disadvantage: limits some compiler optimizations

Java guarantees that operands are evaluated in left-to-right order, eliminating this problem. // C language a = 20; Java a = 8

Assignment Statements
Simple Assignments ƒ The C-based languages use == as the equality relational operator to avoid confusion with their assignment operator. ƒ The operator symbol for assignment: 1. = FORTRAN, BASIC, PL/I, C, C++, Java 2. := ALGOL, Pascal, Ada
Conditional Targets ƒ Ex:

flag ? count 1 : count2 = 0; ⇔ if (flag) count1 = 0; else count2 = 0;
Compound Assignment Operators ƒ A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment. ƒ The form of assignment that can be abbreviated with this technique has the destination var also appearing as the first operand in the expression on the right side, as in

a = a + b

ƒ The syntax of assignment operators that is the catenation of the desired binary operator to the = operator. sum += value; ⇔ sum = sum + value;
Unary Assignment Operators ƒ C-based languages include two special unary operators that are actually abbreviated assignments. ƒ They combine increment and decrement operations with assignments. ƒ The operators ++ and — can be used either in expression or to form standalone single-operator assignment statements. They can appear as prefix operators:

sum = ++ count; ⇔ count = count + 1; sum = count;
ƒ If the same operator is used as a postfix operator:

sum = count ++; ⇔ sum = count; count = count + 1;
Assignment as an Expression

ƒ This design treats the assignment operator much like any other binary operator, except that it has the side effect of changing its left operand. ƒ Ex:

while ((ch = getchar())!=EOF) {…} // why ( ) around assignment?

ƒ The assignment statement must be parenthesized because the precedence of the assignment operator is lower than that of the relational operators. ƒ Disadvantage: Another kind of expression side effect which leads to expressions that are difficult to read and understand. For example

a = b + (c = d / b++) – 1

denotes the instructions Assign b to temp Assign b + 1 to b Assign d / temp to c Assign b + c to temp Assign temp – 1 to a

ƒ There is a loss of error detection in the C design of the assignment operation that frequently leads to program errors.

if (x = y) …

instead of

if (x == y) …

Mixed-Mode Assignment ƒ In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable; whatever conversion is necessary is done. ƒ In Pascal, integers can be assigned to reals, but reals cannot be assigned to integers (the programmer must specify whether the conversion from real to integer is truncated or rounded.) ƒ In Java, only widening assignment coercions are done. ƒ In Ada, there is no assignment coercion. ƒ In all languages that allow mixed-mode assignment, the coercion takes place only after the right side expression has been evaluated. For example, consider the following code:

int a, b; float c; … c = a / b;

Because c is float, the values of a and b could be coerced to float before the division, which could produce a different value for c than if the coercion were delayed (for example, if a were 2 and b were 3).

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to session 7 (expression and assignment statements)

Leave a Reply

Your email address will not be published. Required fields are marked *