Cs 330 organization of programming languages
Sponsored Links
This presentation is the property of its rightful owner.
1 / 19

CS 330 Organization of Programming Languages PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

CS 330 Organization of Programming Languages . Soundararajan Ezekiel Department of Computer Science Ohio Northern University ADA, Ohio 45810 e-mail: [email protected] http://www.onu.edu/user/FS/sezekiel. FORTRAN CODE WALK PROBLEMS.

Download Presentation

CS 330 Organization of Programming Languages

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

CS 330 Organization of Programming Languages

Soundararajan Ezekiel

Department of Computer Science

Ohio Northern University

ADA, Ohio 45810

e-mail: [email protected]



  • 1. Wrtie a program to read two n-dimensional vectors and then evaluate

    • the norm of each vector

    • unit vectors having the same direction as the vectors

    • the sum, difference and dot product

    • the cosine of the angle between the vectors calculated using cos0 = a.b /(|a||b|)

  • 2. Write a program that reads a list of numbers , count them, and the calculate their mean, variance, and standard deviation. Print how many numbers there are and their mean, variance, and SD

Expressions and Assignment Statements

  • Introduction

  • Arithmetic expressions

  • Overloaded Operators

  • Type Conversions

  • Relational and Boolean Expressions

  • Short-Circuit Evaluation

  • Assignment Statements

  • Mixed-Mode Assignments


  • Expressions are the fundamental means of specifying computations in a programming language

  • Programmers should understand both syntax and semantics of expression

  • To understand evaluation -- understand the order of operator and operand evaluation

  • An assignment statement can simply cause a value to be copied from one memory cell to another-- in many case it include expressions with operators, which cause values to be copied to the processor and to be operated on, and the result to be copied back to memory

  • simple assignment statements specify an expression to be evaluated and a target location in which to place the result of the expression evaluation

  • we will see number of variation on this basic form

2. Arithmetic Expression

  • Most of the characteristic of Arithmetic expression were inherited from Math

  • Consists of operators, operands, parentheses, and function calls

  • operator can be

    • unary--- single operands

    • binary -- double operands

    • C, C++, Java, - ternary-- three operands

  • Most cases-- binary operators are infix-- between their operands

  • In Perl-- some of them are prefix-- precede their operands

  • The purpose is to specify an arithmetic computation

  • Implementation of such computation must cause two action

    • fetching the operands-- usually from memory

    • executing arithmetic operations on those operands

  • Design issues

    • 1. What are the operator precedence rules

    • 2. What are the operator associativity rules

    • 3. What is the order of operand evaluation

    • 4. Are there restrictions on operands evaluation side effect

    • 5. Does the language allow user-defined operator overloading

    • 6. What mode mixing is allowed in expression


  • Consider the following expression -- a+b*c

  • suppose a=3, b=4, c=5---- evaluate left to right=> 35, right to left=> 23

  • FORTRAN:- **,* /+-

  • Pascal: *,/,div, mod+ -

  • Ada: **, abs*/ unary +/binary +-

  • C: postfix ++, --prefix ++.-- , unary +-,*/&binary +-

  • APL is odd among languages because it has single level of precedence


  • consider the following expression a-b+c-d

  • here + - are the same level of precedence-- this case which operator is evaluated first is answered by the associativity rule of the language

  • Fortran:- left to right ( exponential right to left A**B**C )

  • Ada: exponential is nonassociative-- A**B**C is illegal () should be used that is (A**B)**C or A** (B**C)

  • FORTRAN and Ada have the exp operator

Language Associativity Rule

FORTRAN Left: */+-

Right: **

Pascal Left: all

C Left: postfix++, postfix--, */%binary+ binary-

Right: prefix++ prefix--, unary + unary-

C++ Left: */%binary+ binary-

Right: ++, --, unary -, unary +

Ada Left: all except **

Nonassociativity **

in APL: AxB+C (x means multiplication) A=3, B=4, C=5 then 27

How: associative right to left-- addition first then multiplication


  • programmers can alter the precedence and associativity rules by placing () in expression --- (A+B)*C

  • programmer would specify the desired order of evaluation with parentheses

  • the disadvantage of this scheme is that it makes writing expression more tedious and it also seriously compromises the readability of the code

Conditional expression

  • we will look at the the ternary operator ?: which is part of C, C++ Java

  • this operator is used to form conditional expression

  • sometimes if then else statements are used to perform a conditional expression assignment

  • example

  • if (count = = 0)

  • average =0;

  • else

  • average= sum/count;

  • In C, C++ and Java this can be specified more conveniently in an assignment statement using condition expression

  • expression_1 ? Expression_2 :expression_3

  • expression_1 is Boolean expression--- if that is true do expression_2 otherwise expression_3

  • average= (count = = 0)? 0 : sum/count;

Operand evaluation order

  • A less commonly discussed design characteristics of expression is the order of evaluation of operands

  • Side Effects:- A side effect of a function called a functional side effect, occurs when the function changes either of its parameters or global variable ( variable declared outside of the function)


  • consider the expression a+fun(a)

  • if fun does not have the side effect of changing a, the order of evaluation is two operands a fun(a) has no effect on value of the expression

  • if fun changes the value of a say divide by 2 and change its parameter to have the value 20 then it matters

  • a=10;

  • b=a+fun(a)

  • two answers--- 15 from left to right and 25 from right to left

C example

  • the following c code will the same problem like before

  • int a=5;

  • int fun1(){

  • a=17;

  • return 3;

  • } /* of fun1*/

  • void fun2(){

  • a=a+fun1();

  • } /* of fun2*/

  • void main(){

  • fun2();

  • }/* of main */

The value computed for a in fun2

depends on the order of evaluation

of the operands in the expression


the value will be 8 or 20


  • 1. The language designer could disallow functional side effect

  • 2. Avoid the problem by stating in the language definition that operands in expressions are to be evaluated in a particular order and demand that implementors guarantee that order

Overloaded operators

  • Arithmetic operators are often used for more than one purpose

  • example:- + for addition In Java it is used for string catenation

  • This multiple use of an operator is called operator overloading


  • consider the use of ampersand in (&) in C

  • As a binary operator--- it specifies bitwise logical AND operation

  • As a Unary operator-- with variable as its operand, the expression value is the address of that variable-- this case & is called address-of operator

  • example x=&y execution of this causes the address of y to be placed in x

  • Login