Session 5

Expression and Assignment Statements

October 21st, 2017

on session 4, we talk more about Expression and Assignment statement, what is Expression ? and what is Assignment statement in computing ?

Expressions are the fundamental means of specifying computations in a programming language.To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation.Essence of imperative languages is dominant role of assignment statements.

here are types of expression in computing :

I. Arithmetic Expressions
Arithmetic evaluation was one of the motivations for the development of the first programming languages
Arithmetic expressions consist of operators, operands, parentheses, and function calls

  • A unary operator has one operand
  • A binary operator has two operands
  • A ternary operator has three operandsOperator Precedence Rules
    • Typical precedence levels

    – parentheses

    – unary operators

    – ** (if the language supports it)

    – *, /

    – +, –

    Operator Associativity Rule

    • The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated
    • Typical associativity rules

    –Left to right, except **, which is right to left

    –Sometimes unary operators associate right to left (e.g., in FORTRAN)

    • APL is different; all operators have equal precedence and all operators associate right to left
    • Precedence and associativity rules can be overriden with parentheses


    Operand evaluation order
    1.Variables: fetch the value from memory

    2.Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction

    3.Parenthesized expressions: evaluate all operands and operators first

    4.The most interesting case is when an operand is a function call

     

     

II. Functional Side Effects

Two possible solutions to the problem

1.Write the language definition to disallow functional side effects

  • No two-way parameters in functions
  • No non-local references in functions
  • Advantage: it works!
  • Disadvantage: inflexibility of one-way parameters and lack of non-local references

2.Write the language definition to demand that operand evaluation order be fixed

  • Disadvantage: limits some compiler optimizations
  • Java requires that operands appear to be evaluated in left-to-right order

Overloaded Operators

  • Use of an operator for more than one purpose is called operator overloading
  • Some are common (e.g., + for int and float)
  • Some are potential trouble (e.g., * in C and C++)

–Loss of compiler error detection (omission of an operand should be a detectable error)

–Some loss of readability

  • C++, C#, and F# allow user-defined overloaded operators

–When sensibly used, such operators can be an aid to readability (avoid method calls, expressions appear natural)

–Potential problems:

  • Users can define nonsense operations
  • Readability may suffer, even when the operators make sense

III. Type Conversion

  • A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int
  • 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

IV. Errors in Expressions

  • Causes

–Inherent limitations of arithmetic                         e.g., division by zero

–Limitations of computer arithmetic                     e.g. overflow

  • Often ignored by the run-time system

V. Relational and Boolean Expressions

  • Relational Expressions

–Use relational operators and operands of various types

–Evaluate to some Boolean representation

–Operator symbols used vary somewhat among languages (!=, /=, ~=, .NE., <>, #)

  • Boolean Expressions

–Operands are Boolean and the result is Boolean

–Example operators

 

Sources: https://binusmaya.binus.ac.id/newStudent/#/class/resources.COMP6060/007420/1710/LEC/15077

 

 

This entry was posted in Session Summary. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *