Assignment Statements and Arithmetic Expressions

41
Assignment Statements and Arithmetic Expressions

description

Assignment Statements and Arithmetic Expressions. Assignment Statements. Change the value of a variable Cause a value to be copied from one memory cell to another Specify an expression to be evaluated and stored into a target location. Arithmetic Expressions. - PowerPoint PPT Presentation

Transcript of Assignment Statements and Arithmetic Expressions

Page 1: Assignment Statements and Arithmetic Expressions

Assignment Statements and Arithmetic Expressions

Page 2: Assignment Statements and Arithmetic Expressions

Assignment Statements

• Change the value of a variable

• Cause a value to be copied from one memory cell to another

• Specify an expression to be evaluated and stored into a target location

Page 3: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions

The purpose of an arithmetic expressions is to specify an arithmetic computation.

The implementation of the computation involves: • fetching the operands• executing the arithmetic operations

Page 4: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions

Arithmetic expressions are constructions of:

• operators• operands• parentheses• function calls

Page 5: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions

The operators can be

• unary• binary• ternary

Page 6: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions:

Operator evaluation order

• Precedence - defines the order in which operators of different precedence are evaluated

• Associativity - defines the order in which operators of equal precedence are evaluated

• Parentheses - default evaluation order can be overridden with use of parentheses

Page 7: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Precedence

FORTRANhighest: ** (exponentiation)

*, / (multiplication, division)

all +, - (unary and binary addition and subtraction)

lowest:

Page 8: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Precedence

Pascalhighest: *, /,div, mod

all +, - lowest:

Page 9: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Precedence

Chighest: postfix ++, --

prefix ++, --unary +, -*, /, %binary +, -

lowest:

Page 10: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: associativity

Left associativity - leftmost operator is evaluated first

A / B * C (FORTRAN)

Right associativity - rightmost operator is evaluated first

A ** B ** C (FORTRAN)

Non-associativity - operators of equal precedence must be parenthesized

A ** (B ** C) (ADA for exponentiation)

Page 11: Assignment Statements and Arithmetic Expressions

Sequence Control for Arithmetic Expressions

Tree Structure Representation• clarifies control structure of an expression• syntactic representation options

Execution-time Representation• machine code• evaluation of tree structures• prefix or postfix form

Page 12: Assignment Statements and Arithmetic Expressions

Tree Structure Representation

• prefix (Polish prefix) notation

• infix

• postfix or reverse Polish notation (RPN)

Page 13: Assignment Statements and Arithmetic Expressions

Tree Structure RepresentationSyntactic Representation Options

• prefix (Polish prefix) notation the operator comes first, followed by the operands

- same notation as function calls f(x, y, z)- no parenthesis needed- relatively simple translation process- unique operators needed for operations with variable number of operands- lack of structuring cues (reduces readability)- number of operands must be known

Page 14: Assignment Statements and Arithmetic Expressions

Tree Structure RepresentationSyntactic Representation Options

• infix for binary operations, the operator is written between the two operands

- gives natural representation (readability)- best suited for binary operations- requires complex translation process

Page 15: Assignment Statements and Arithmetic Expressions

Syntactic Representation Options

• postfix or reverse Polish notation (RPN) the operands are written first, followed by the operator

- advantages and disadvantages similar to prefix

Tree Structure Representation

Page 16: Assignment Statements and Arithmetic Expressions

(a + b) x (c - a)

x

+ -

cba a

Tree Structure Representation

Page 17: Assignment Statements and Arithmetic Expressions

(a + b) x (c - a)

x

+ -

cba a

Prefix?

Infix?

Postfix?

Tree Structure Representation

Page 18: Assignment Statements and Arithmetic Expressions

Evaluations of Expressions

Postfix evaluation: (evaluate using an execution stack)

1. Scan expression left to right2. If OPERAND, push onto stack.3. If OPERATOR, pop the corresponding number of arguments off the stack, apply the operator to the operands.4. Push result onto stack as next operand.

Page 19: Assignment Statements and Arithmetic Expressions

• Find the postfix representation

• Evaluate the postfix expression using an execution stack.

(a + b) x (c - a), let a=3, b=4, c=5

-b + b - 4ac let a=2, b=-7, c=3

2a

2

Evaluations of Expressions

Page 20: Assignment Statements and Arithmetic Expressions

Prefix evaluation: (evaluate using an execution stack)

1. Scan expression left to right2. If OPERATOR, push onto stack. Set argument count to n, number of arguments req’d by operator3. If OPERAND, push onto stack.4. If top n entries are operands, pop the top n entries, pop the operator and apply the operator to those operands.5. Push result onto stack as next operand.

Evaluations of Expressions

Page 21: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions:

Operand evaluation order: side effects

A := 10;NEW := A + fun(A);

Suppose function fun returns the value of its argument divided by two. And suppose as a side effect, it changes the value of its argument to 20.

Page 22: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: a ternary operator

Conditional expression: C and C++:

expression1 ? expression2 : expression3

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

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

Page 23: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Overloaded Operators

Arithmetic operators are often used for more than one purpose

This may adversely affect:• readabilty

same symbol is used for unrelated operations• reliability

increases the chance that a typo will compile

Page 24: Assignment Statements and Arithmetic Expressions

Arithmetic operators are often used for more than one purpose examples:

+ for integer and floating point addition, string catenation in C++

& for addressing and bitwise and operation in C

/ for integer and floating point division in C++

Arithmetic Expressions: Overloaded Operators

Page 25: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Overloaded Operators

Arithmetic operators may also be overloaded by the user in C++

Properly used this may aid readabilty and writability:example: +, * overloaded for a user defined matrix data type a * b + c * d instead of MatrixAdd(MatrixMult(a,b), MatrixMult(c,d))

Page 26: Assignment Statements and Arithmetic Expressions

Arithmetic Expressions: Type Conversions

• narrowing• widening

A narrowing conversion is one that converts an objects to a type that cannot include all of the values of the original type

A widening conversion is one in which an object is converted to a type that can include at least approximations of all the values of the original type

Page 27: Assignment Statements and Arithmetic Expressions

When arithmetic operations include operands of different types (mixed-mode expressions) implicit type conversions must be performed.

A coercion is an implicit type conversion performed by the compiler.

Arithmetic Expressions: Type Conversions

Page 28: Assignment Statements and Arithmetic Expressions

Explicit Type Conversion

• Most languages allow for explicit type conversion.

• Some provide a warning when the conversion is narrowing and significant change in value will result.

ADA: AVG := FLOAT(SUM) / FLOAT(COUNT)C: avg = (float) sum / count

Arithmetic Expressions: Type Conversions

Page 29: Assignment Statements and Arithmetic Expressions

• operand type errors (avoided if type checking is used).

• coercion

• limitations of computer arithmeticº integer overflowº floating point overflow

• inherent limitations of arithmetic

Arithmetic Expressions: Errors

Page 30: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

• A relational expression has two operands and one relational operator

Relational Expressions• A relational expression has two operands

and one relational operator

• The value produced by a relational operator is boolean (unless boolean is not a type in the language)

Page 31: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Relational Operators

Pascal FORTRAN C = .EQ. == <> .NE. != <= .LE. <= < .LT. < >= .GE. >= > .GT. !>

Page 32: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Boolean Expressions consist of

• boolean variables• boolean constants• relational expressions• boolean operators

used for control structures

Page 33: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Boolean Operators

Pascal FORTRAN C not .NOT. ! and .AND. && or .OR. ||

Page 34: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

The precedence of boolean operators is normally

not - highestand or - lowest

but the precedence of relational operators and arithmetic operators as compared to boolean operators, differs according to each language.

Page 35: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

FORTRAN 77:Highest **

*, /+, -.EQ., .NE., .GT., .GE., .LT., .LE..NOT..AND..OR.

Lowest .EQV., .NEQV. (logical compare)

Page 36: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Pascal:

Highest not*, /, div, mod, and+, -, or

Lowest =, <>, <, <=, >, >=, in

Page 37: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

C:Highest !, ~ *, /, %

+, -<, <=, >, >===, !=&^|&&

Lowest ||

Page 38: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Pascal:j >= 7 and k<5

FORTRAN:J.GE.7 .AND. K.LT.5

C/C++:j >= 7 && k<5

Pascal:j >= 7 and k<5 <- Syntax error

FORTRAN:J.GE.7 .AND. K.LT.5

C/C++:j >= 7 && k<5

precedence

Page 39: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

FORTRAN:1 .LE. J .LE. 10

Pascal:1 <= j <= 10 <- Syntax error

C/C++:1 <= j <= 10

<- Syntax error

<- True

type conversion

Page 40: Assignment Statements and Arithmetic Expressions

Boolean and Relational Expressions:

Pascal: (most implementations do not use short circuit)

while (count <> 0) and (sum/count > 100) do

Short-circuit evaluation of an expression - result is determined without evaluating all operators

Short-circuit evaluation

C/C++/Java: (&&, || use short circuit but &, | do not)

while ((count != 0) && (sum/count > 100))

Page 41: Assignment Statements and Arithmetic Expressions

Assignment Statements:

• simple assignment• multiple assignments• conditional assignment• compound assignment• unary assignment• assignment as an expression

Options and side effects