 # Chapter 7 Expressions and Assignment Statements

Yüklə 447 b.
 tarix 17.11.2018 ölçüsü 447 b. • ## Mixed-Mode Assignment • ## It is important to understand the syntax and semantics of expressions. • ## Design issues for arithmetic expressions

• operator precedence rules
• operator associativity rules
• order of operand evaluation
• operand evaluation side effects
• mode mixing expressions • ## A ternary operator has three operands. For example, the C-based languages ternary operator ?: • ## Typical precedence levels

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

• ## A + (- B) * C • ## Thus, the order of evaluation of operators in APL expression is determined by the associativity rule. • ## 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.
• ## If neither of the operands of an operator has side effects, then operand evaluation order is irrelevent. • ## Consider the expression

• a + fun(a)

• ## If fun changes a, there is an effect. • ## 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);

• ## If the second operand (fun(a)), is evaluated first, then the value of the first operand is 20 and the value of the expression is 25. • ## 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. • ## A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type, e.g. int to float • ## In most languages, all numeric types are coerced in expressions, using widening conversions. • ## Examples

• C: (int) angle
• ## Note that Ada’s syntax is similar to function calls • ## 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;
• ## Will not cause an error when Key is not in List and Index becomes larger than Listlen. • ## The assignment operator

• = FORTRAN, BASIC, PL/I, C, C++, Java
• ## = can be bad when it is overloaded for the relational operator for equality. • ## 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) • ## ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement • int a, b;
• float c;
• c = a / b;

• ## In Ada, there is no assignment coercion. • ## 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ə