# Chapter 7 Expressions and Assignment Statements

Yüklə 447 b.
 tarix 17.11.2018 ölçüsü 447 b.

• ## Design issues for arithmetic expressions

• operator precedence rules
• operator associativity rules
• order of operand evaluation
• operand evaluation side effects
• mode mixing expressions

• ## Typical precedence levels

• parentheses
• unary operators
• ** (if the language supports it)
• *, /
• +, -

• ## Typical associativity rules

• Left to right, except **, which is right to left
• Sometimes unary operators associate right to left (e.g., in FORTRAN)

• ## Precedence and associativity rules can be overridden with parentheses. For example

• A – B + C equivalent to (A - B) + C
• A ** B ** C equivalent to A ** ( B ** C)

• ## average = (count == 0)? 0 : sum / count

• Evaluates as
• if (count == 0) average = 0
• else average = sum /count

• ## Operand evaluation order

• Variables: fetch the value from memory
• Constants: sometimes fetch from memory; sometimes the constant is in the machine language instruction.
• Parenthesized expressions: evaluate all operands and operators first.

• ## Consider the expression

• a + fun(a)

• ## Consider the following situation: fun returns the value of its arqument divided by 2 and changes the value of its parameter to 20. Suppose we have the following

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

• ## Two possible solutions to the problem

• Write the language definition to disallow functional side effects
• Disadvantage: inflexibility of two-way parameters and global references.
• Write the language definition to demand that operand evaluation order be fixed.
• Disadvantage: limits some compiler optimizations.

• ## Some are potential trouble

• For example the ampersand operator (&) in C.
• A binary (&) specifies a bitwise logical AND operator.
• A unary (&) specifies an address (x = &y).
• ## Can be avoided by introduction of new symbols

• For example, Pascal use div for integer division and / for float division.

• ## Potential problems:

• Users can define nonsense operations, define + for multiplication.
• Readability may suffer, even when the operators make sense. For example, seeing an * operator in a program, the reader must find both the types of the operands and the definition of the operator.

• ## Examples

• C: (int) angle

• ## Causes

• Limitations of arithmetic
• e.g., division by zero
• Limitations of computer arithmetic, when the result cannot be represented in memory cell
• e.g. overflow and underflow.

• ## Relational Expressions

• Use relational operators and operands of various types
• Evaluate to some Boolean representation
• Operator symbols used vary somewhat among languages
• For example, the operator “not equal” is represented differently in many language:
• (!=, /=, .NE., <>, #)
• ## The relational operators always have lower precedence than the arithmetic operators, for example

• a + 1 > 2 * b
• The arithmetic expression are evaluated first.

• ## Boolean Expressions

• Operands are Boolean and the result is Boolean
• Example operators
• FORTRAN 77 FORTRAN 90 C Ada
• .AND. and && and
• .OR. or || or
• .NOT. not ! not
• xor

• ## One odd characteristic of C’s expressions: a < b < c is a legal expression, but the result is not what you might expect:

• Left operator is evaluated, producing 0 or 1
• The evaluation result is then compared with the third operand (i.e., c)

• ## Precedence of C-based operators

• prefix ++, --
• unary +, -, prefix ++, --, !
• *,/,%
• binary +, -
• <, >, <=, >=
• =, !=
• &&
• ||

• ## Example: (13*a) * (b/13–1)

• If a is zero, there is no need to evaluate (b/13-1)
• However, in arithmetic expressions this is not easily detected during execution, so it is never taken.
• ## Example: (a >= 0) && (b < 10)

• If a < 0, there is no need to evaluate (b < 10)
• Unlike the case of arithmetic expression, this shortcut can be easily discovered during execution.

• ## Assume list is an array of length elements (list has length -1 as the upper-bound subscript value)

• index = 1;
• while (index < length) && (list [index] != key)
• index++;
• If evaluation is not short-circuit, both relational expressions are evaluated.
• If key is not in list then index=length, and list [index] will cause an indexing problem (subscript out-of-range error).

• ## For example, in Ada assume that List is declared to have a subscript range of 1 .. Listlen, the Ad code

• Index := 1;
• while (Index <= Listlen) and then (List (Index) /= Key)
• loop
• Index := Index + 1;
• end loop;

• ## The assignment operator

• = FORTRAN, BASIC, PL/I, C, C++, Java

• ## C and C++ allows Conditional targets on assignment statements.

• (flag)? total : subtotal = 0
• ## Which is equivalent to

• if (flag)
• total = 0
• else
• subtotal = 0

• ## Example

• a = a + b is written as a += b

• ## Examples

• sum = ++count; is equivalent to
• count = count + 1;
• sum = count;
• sum = count++ ; is equivalent to
• sum = count;
• count = count + 1;
• count++; (count incremented)
• -count++; (count incremented then negated)

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

• ## Various forms of assignment

Dostları ilə paylaş:

Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2019
rəhbərliyinə müraciət

Ana səhifə