Sebesta 7

advertisement
Ryan Chu
Arithmetic Expressions
 Arithmetic expressions consist of operators, operands,
parentheses, and function calls. The purpose is to
specify an arithmetic computation.
 Operators
 Unary : A single operand
 Binary : Two operands
 Ternary : Three operands
Operator Evaluation Order
 Precedence
 The operator precedence rules for expression evaluation
define the order in which the operators of different
precedence levels are evaluated.
 In most languages, exponentiation has the highest
precedence, followed by multiplication and division on
the same level, followed by binary addition and
subtraction on the same level.
 Identity operator : Unary versions of addition and
subtraction.
Operator Evaluation Order
 Associativity
 The associativity rules of the language define the order
when an expression contains two adjacent occurrences
of operators with the same level of precedence.
 Associativity in common imperative languages is left to
right, except that the exponentiation operator associates
right to left.
A–B+C
Left to Right
A ** B ** C
Right to Left
Operator Evaluation Order
 Parentheses
 A parenthesized part of an expression has precedence
over its adjacent unparenthesized parts.
 Conditional Expressions
 If-then-else
 If ( count == 0 )
Average = 0;
Else
Average = sum / count;
Operand Evaluation Order
 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.
 Fun returns the value of its argument divided by 2 and
changes its parameter to have the value 20.
a = 10;
b = a + fun(a);
If the first operand is evaluated first, a is 10 and the value of
the expression is 15.
If the second operand is evaluated first, then the value of the
first operand is 20 and the value of the expression is 25.
Operand Evaluation Order
 Solutions to side effects
 The language designer could disallow function
evaluation from affecting the value of expressions by
simply disallowing functional side effects.
 State in the language definition that operands in
expressions are to be evaluated in a particular order and
demand that implementers guarantee that order.
Overloaded Operators
 Operator overloading is when an operator is used for more
than one purpose and is generally thought to be acceptable,
as long as readability and/or reliability do not suffer.
 Issues
 Consider “&” in C. As a binary operator, it specifies a bitwise
logical AND operation. However, as a unary operator with a
variable as its operand, the expression value is the address of
that variable. Since the same symbol is used for two
completely unrelated operations, the simple keying error of
leaving out the fist operand for a bitwise AND operation can
go undetected by the compiler.
Type conversions
 A narrowing conversion converts a value to a type that
cannot store even approximations of all of the values of
the original type.
 A widening conversion converts a value to a type that
can include at least approximations of all of the values
of the original type.
 Type conversions can be either explicit or implicit.
Relational and Boolean Expressions
 Relational Expressions
 A relational operator is an operator that compares the
values of its two operands. A relational expression has
two operands and one relational operator.
 The relational operators always have lower precedence
than the arithmetic operations.
a+1>2*b
The arithmetic expressions are evaluated first.
Relational and Boolean Expressions
 Boolean Expressions
 Boolean expressions consist of Boolean variables,
Boolean constants, relational expressions, and Boolean
operators.
 In most of the common imperative languages, the unary
NOT has the highest precedence, followed by AND at a
separate level, and OR at the lowest level.
 C has no Boolean type and thus no Boolean values.
Instead, numeric values are used to represent Boolean
values. ( false = 0 and true = all nonzero)
a>b>c
Short-Circuit Evaluation
 A short-circuit evaluation of an expression is one in
which the result is determined without evaluating all
of the operands and/or operators.
 (13 * a) * (b / 13 – 1)
However, in arithmetic expressions this shortcut is not
easily detected during execution, so it is never taken.
 (a >= 0) and (b < 10)
Unlike the case of arithmetic expressions, this shortcut
can be easily discovered during execution and taken.
Assignment Statements
 It provides the mechanism by which the user can
dynamically change the bindings of values to variables.
 Simple Assignments
 <target_variable> <assignment_operator> <expression>
 Multiple Targets
 SUM, TOTAL = 0
 Conditional Targets
 If (flag) count1 = 0 ; else count2 = 0;
 Compound Assignment Operators
 sum = sum + value; => sum += value;
Assignment Statements
 Unary Assignment Operator
 They can appear as either prefix operators, meaning they
precede the operands, or as postfix operators, meaning
they follow the operands.
sum = ++ count;
sum = count ++;
 When two unary operators apply to the same operand,
the association is right to left.
- count ++;
Assignment Statements
 Assignment as an Expression
 The assignment statement produces a result, which is
the same as the value assigned to the target.
a = b + (c = d / b++) – 1
Assign b to temp
Assign b + 1 to b
Assign d / temp to c
Assign b + c to temp
Assign temp -1 to a
Questions?
Download