ryan chu. arithmetic expressions arithmetic expressions consist of operators, operands, parentheses,...

16
Ryan Chu

Upload: hugo-collins

Post on 11-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Ryan Chu

Page 2: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Arithmetic ExpressionsArithmetic expressions consist of operators,

operands, parentheses, and function calls. The purpose is to specify an arithmetic computation.

Operators Unary : A single operandBinary : Two operandsTernary : Three operands

Page 3: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Operator Evaluation OrderPrecedence

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.

Page 4: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Operator Evaluation OrderAssociativity

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 RightA ** B ** C Right to Left

Page 5: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Operator Evaluation OrderParentheses

A parenthesized part of an expression has precedence over its adjacent unparenthesized parts.

Conditional ExpressionsIf-then-else If ( count == 0 )

Average = 0;Else

Average = sum / count;

Page 6: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Operand Evaluation OrderSide 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.

Page 7: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Operand Evaluation OrderSolutions 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.

Page 8: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Overloaded OperatorsOperator 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.

IssuesConsider “&” 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.

Page 9: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Type conversionsA 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.

Page 10: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Relational and Boolean ExpressionsRelational 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 * bThe arithmetic expressions are evaluated first.

Page 11: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Boolean ExpressionsBoolean 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

Relational and Boolean Expressions

Page 12: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Short-Circuit EvaluationA 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.

Page 13: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Assignment StatementsIt 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 = 0Conditional Targets

If (flag) count1 = 0 ; else count2 = 0;Compound Assignment Operators

sum = sum + value; => sum += value;

Page 14: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Assignment StatementsUnary 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 ++;

Page 15: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an

Assignment StatementsAssignment 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++) – 1Assign b to tempAssign b + 1 to bAssign d / temp to cAssign b + c to tempAssign temp -1 to a

Page 16: Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an