Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.

Slides:



Advertisements
Similar presentations
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
COEN Expressions and Assignment
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CS2403 Programming Languages Expressions and Assignment Statements Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are.
CSE 452: Programming Languages Expressions and Control Flow.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
C HAPTER 7 Expressions and Assignment Statements.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
1 CS Programming Languages Class 10 September 26, 2000.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions & Assignments
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Expressions and Assignment Statements
Expression and Asignment Statements
Chapter 7 Expressions and Assignment Statements.
Chapter 7: Expressions and Assignment Statements Sangho Ha
B=15 b = a + fun(a); a=10 fun(a)=5.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much from math conventions –expressions are constructed from math operators, operands, parentheses, functions Design issues: –what are the precedence and associativity rules? –what order are operands evaluated? –are there side effects? –is operator-overloading available? –what type mixing is allowed? All expressions consist of operators and operands, 3 forms of operators: –Binary (2 operands, the standard form of mathematical operator) –Unary (1 operand as in C’s i++ or in unary – as in -5) –Ternary (3 operands, as in C’s conditional statement) Expressions requiring fetching the operator followed by operands, then applying the operator to those operands

Precedence Rules and Associativity Highest ** postfix ++, -- **, abs ** *, / prefix ++, -- *, /, mod, rem unary +, - +, - unary +, - unary +,- *, /, % *, /, % binary +,- binary +, - Lowest binary +, - FORTRAN C-languages AdaRuby “(” and “)” have the highest precedence in all languages If two or more operators are of the same precedence, then they are applied based on associativity –all languages use left-to-right associativity except when dealing with exponent and unary operators in FORTRAN, exponent is the only right-to-left operator in Ada, a**b**c is illegal and so most be parenthesized –NOTE: in Ada, unary – has lower precedence than mod, so –a mod b is not the same as –(a mod b) in C-langauges, ++/-- and unary +/- are right associative and there is no exponent operator In VB, exponent (^) is left associative

Side Effects A side effect is when a function changes a parameter passed to it –or changes a global variable This may lead to associativity not being maintained, for instance in –A + Fun(A) if Fun(A) does not alter A, then the order of evaluation between A and Fun(A) is unimportant but if Fun changes A, then A + Fun(A) Fun(A) + A Side effects can occur when the parameter is passed by reference (a pointer) –side effects become unimportant if you require that any function call be located at the end of an expression –some languages disallow side effects (that is, you are not able to pass pointers to pointers to functions, or use global vars in functions) Here is how some languages deal with side effects –C/C++: side effects are allowed to leave the language flexible –FORTRAN 77: expressions with function calls are legal only if the function does not change the value of any operands in the expression –Pascal and Ada: allow all side effects –Java: operands evaluated left-to-right, programmer can avoid side effects by placing function calls to the right in an expression

Overload Operators When the same operator is used for different types –arithmetic operators (+, -, *, /) are all used for byte, short, int, long, float and double, so they are all overloaded however, for each, the implementation differs –overloading can aid readability and writability but requires type checking + may also be used for and, union or string concatenation * is overloaded in C/C++ for pointer dereferencing & is overloaded in C/C++ to generate an address and bitwise AND –in cases where overloading causes the type of operation to differ (addition versus concatenation, dereferencing versus multiplication), then overloading damages readability the compiler must choose the correct meaning of the operator given operand types Question: can the programmer overload an operator? –available in Ada, C++, FORTRAN 95, Common Lisp, C# C++ restricts which operators can be overloaded (.,.*, ::, ?:, sizeof)

Type Conversions/Coercions Narrowing – going from a larger type to smaller type (e.g. int to short int) –narrowing is rarely safe Widening – going from a smaller type to larger –widening is almost always safe however consider going from a 32-bit int to a 32-bit float, while this is thought of as widening, you might lose precision! Coercion: –implicit type conversion initiated by the compiler for instance, doing x + y where x is an int and y is a float Casts (in C/C++, Java, C#) are explicit conversion commands Ada and Modula-2 also have them as in Float(X) –Ada does not do implicit coercions, so the assignment below is not legal since * cannot apply to both an integer and a float A : Integer; B, C : Float; C = A * B; Java also has methods to perform explicit conversions Most languages have at least round and truncate operations

Relational and Boolean Expressions Evaluate to True/False rather than numeric –relational operators always have lower precedence than arithmetic so that the relational comparison is performed after any arithmetic operations (ex. a+1 > b-2) –boolean operators usually have lower precedence than relational operators except possibly NOT OperationAda C/Java FORTRAN77 Equal== =.EQ. Not equal/= !=.NE. Greater Than> >.GT. Less Than< <.LT. Greater/Equal>= >=.GE. Less/Equal<= <=.LE. ANDand &&.AND. ORor | |.OR. NOT not !.NOT. The FORTRAN abbreviations originated because early keypunches did not have symbols available FORTRAN 95 continued to use the abbreviations, but also included = =, <>,, = JavaScript/PHP add = = = and != = for equality/inequality with coercion disabled

Example expressions In FORTRAN: –A+B.GT. 2 * C. AND. K.NE. 0 order of operations: *, +, GT, NE, AND In Ada –A > B and A < C or K=0 is illegal because and/or have equal precedence and therefore must parenthesize, the proper expression is –(A > B and A < C) or K = 0 with order of precedence as and = performed first in a left-to-right order, followed by and followed by or In C: a > b > c –is legal as a > b returns a 1 or 0 which is then compared to c C-language operator precedence postfix++,-- prefix ++, --, ! unary +, - *, /, % binary +, -, = = =, != && | |

Short Circuiting A short circuited evaluation of an expression occurs if the result can be determined without evaluating the entire expression –(13 * A) * (B / ) if A=0, then the expression evaluates to 0 and the right term (B / …) is ignored –(A >= 0) and (B < 10) if A < 0 then the entire expression is false We often take advantage of short circuiting to simplify code to avoid errors –while(ptr!=NULL && ptr->value > target) ptr = ptr->next; –without short circuiting, we either risk an error arising (if ptr is NULL and we still try to evaluat ptr->value, then we get an error) or we have to separate this into a while loop with an if statement inside it In Ada, short-circuiting is available using AND THEN and OR ELSE –While (Index <= Listlen) and then (List[index] /= key) do In Modula-2, Turbo Pascal, AND and OR are short-circuited –this is not true of Standard Pascal In C, C++, Java, && and || are short circuited and & and | are not –one must be cautious in C-languages when using short circuiting, consider this: –(a > b) && ((b++) / 3) if a > b is false, the right side of the expression is not performed and so b remains unchanged!

The Assignment Statement Syntactic form: – FORTRAN, BASIC, PL/I, C, C++, Java, C#, Python all use = for whereas Ada/Algol/Pascal/Modula all use := –in PL/I, the use of = is confusing as it is also used for “equal to” leading to A = B = C this means “set A equal to true if B = C or false if B does not equal C” Some languages allow for multiple assignments in one statement –PL/I: SUM, TOTAL = 0 –C, C++, Java can use incr/decr operators and nested assignments: a = b+(c = d / b++) - 1 –b gets b+1, c gets d / b (old b), a gets b + c - 1 (old b, old c) while((ch = getchar( )) != EOF) {…} –Python: a, b, c =,, –Common Lisp: (setf a b c d) same as a=b, c=d –Perl: ($a, $b, $c) = (1, 2, 3); C/C++/ Java also have the conditional target (ternary operator) –flag ? count1 : count2 = 0