Assignment Statements and Arithmetic Expressions Assignment Statements Change

  • Slides: 34
Download presentation
Assignment Statements and Arithmetic Expressions

Assignment Statements and Arithmetic Expressions

Assignment Statements • Change the value of a variable • Cause a value to

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 The purpose of an arithmetic expressions is to specify an arithmetic computation.

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

Arithmetic Expressions Arithmetic expressions are constructions of: • • operators operands parentheses function calls

Arithmetic Expressions Arithmetic expressions are constructions of: • • operators operands parentheses function calls

Arithmetic Expressions The operators can be • unary • binary • ternary

Arithmetic Expressions The operators can be • unary • binary • ternary

Arithmetic Expressions: Operator evaluation order • Precedence - defines the order in which operators

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

Arithmetic Expressions: Precedence FORTRAN highest: ** *, / all +, lowest: (exponentiation) (multiplication, division)

Arithmetic Expressions: Precedence FORTRAN highest: ** *, / all +, lowest: (exponentiation) (multiplication, division) (unary and binary addition and subtraction)

Arithmetic Expressions: Precedence Pascal highest: *, /, div, mod all +, - lowest:

Arithmetic Expressions: Precedence Pascal highest: *, /, div, mod all +, - lowest:

Arithmetic Expressions: Precedence C highest: lowest: postfix ++, -prefix ++, -unary +, *, /,

Arithmetic Expressions: Precedence C highest: lowest: postfix ++, -prefix ++, -unary +, *, /, % binary +, -

Arithmetic Expressions: associativity Left associativity - leftmost operator is evaluated first A/B*C (FORTRAN) Right

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)

Sequence Control for Arithmetic Expressions Tree Structure Representation • clarifies control structure of an

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

Tree Structure Representation • prefix (Polish prefix) notation • infix • postfix or reverse

Tree Structure Representation • prefix (Polish prefix) notation • infix • postfix or reverse Polish notation (RPN)

Tree Structure Representation Syntactic Representation Options • prefix (Polish prefix) notation the operator comes

Tree Structure Representation Syntactic 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

Tree Structure Representation Syntactic Representation Options • infix for binary operations, the operator is

Tree Structure Representation Syntactic 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

Tree Structure Representation Syntactic Representation Options • postfix or reverse Polish notation (RPN) the

Tree Structure Representation 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 (a + b) x (c - a) x + a b

Tree Structure Representation (a + b) x (c - a) x + a b c a

Tree Structure Representation (a + b) x (c - a) Prefix? x Infix? Postfix?

Tree Structure Representation (a + b) x (c - a) Prefix? x Infix? Postfix? + a b c a

Evaluations of Expressions Postfix evaluation: (evaluate using an execution stack) 1. Scan expression left

Evaluations of Expressions Postfix evaluation: (evaluate using an execution stack) 1. Scan expression left to right 2. 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.

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

Evaluations of Expressions (a + b) x (c - a), let a=3, b=4, c=5 • Find the postfix representation • Evaluate the postfix expression using an execution stack. -b + 2 b - 4 ac 2 a let a=2, b=-7, c=3

Evaluations of Expressions Prefix evaluation: (evaluate using an execution stack) 1. Scan expression left

Evaluations of Expressions Prefix evaluation: (evaluate using an execution stack) 1. Scan expression left to right 2. If OPERATOR, push onto stack. Set argument count to n, number of arguments req’d by operator 3. 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.

Arithmetic Expressions: Operand evaluation order: side effects A : = 10; NEW : =

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.

Arithmetic Expressions: Conditional expression: C and C++: expression 1 ? expression 2 : expression

Arithmetic Expressions: Conditional expression: C and C++: expression 1 ? expression 2 : expression 3 avg = (count == 0) ? 0 : sum / count; if (count == 0) avg = 0; else avg = sum / count;

Arithmetic Expressions: Overloaded Operators Arithmetic operators are often used for more than one purpose

Arithmetic Expressions: Overloaded Operators Arithmetic operators are often used for more than one purpose examples: + for integer and floating point addition, string catenation & for addressing and bitwise and operation in C

Arithmetic Expressions: Type Conversions • • narrowing widening A narrowing conversion is one that

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

Arithmetic Expressions: Type Coercion When arithmetic operations include operands of different types (mixed-mode expressions)

Arithmetic Expressions: Type Coercion 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: Explicit Type Conversion • Most languages allow for explicit type conversion. •

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

Boolean and Relational Expressions: Relational Expressions A relational operator compares the values of its

Boolean and Relational Expressions: Relational Expressions A relational operator compares the values of its two operands 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)

Boolean and Relational Expressions: Relational Operators Pascal = <> <= < >= > FORTRAN.

Boolean and Relational Expressions: Relational Operators Pascal = <> <= < >= > FORTRAN. EQ. . NE. . LT. . GE. . GT. C == != <= < >= !>

Boolean and Relational Expressions: Boolean Expressions consist of boolean variables boolean constants relational expressions

Boolean and Relational Expressions: Boolean Expressions consist of boolean variables boolean constants relational expressions and boolean operators

Boolean and Relational Expressions: Boolean Operators Pascal not and or FORTRAN. NOT. . AND.

Boolean and Relational Expressions: Boolean Operators Pascal not and or FORTRAN. NOT. . AND. . OR. C ! && ||

Boolean and Relational Expressions: The precedence of boolean operators is normally not - highest

Boolean and Relational Expressions: The precedence of boolean operators is normally not - highest and or - lowest but the precedence of relational operators and arithmetic operators as compared to boolean operators, differs according to each language.

Boolean and Relational Expressions: FORTRAN 77: Highest ** *, / +, // (string catenation).

Boolean and Relational Expressions: FORTRAN 77: Highest ** *, / +, // (string catenation). EQ. , . NE. , . GT. , . GE. , . LT. , . LE. . NOT. . AND. . OR. Lowest. EQV. , . NEQV. (logical compare)

Boolean and Relational Expressions: Pascal: Highest Lowest not *, /, div, mod, and +,

Boolean and Relational Expressions: Pascal: Highest Lowest not *, /, div, mod, and +, -, or =, <>, <, <=, >, >=, in

Boolean and Relational Expressions: C: Highest Lowest !, ~, ++, ---, sizeof(), (type), +,

Boolean and Relational Expressions: C: Highest Lowest !, ~, ++, ---, sizeof(), (type), +, -(unary), *(indirection), &(address) *, /, % +, >>, << <, <=, >, >= ==, != & ^ | && ||