Chapter 7: Expressions and Assignment Statements

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

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
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
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
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.
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.
1 Chapter 7 Expressions and Assignment statements.
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
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
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
Data Types and Expressions
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
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Chapter 7: Expressions and Assignment Statements Lectures # 14

Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment Chapter 7: Expressions and Assignment Statements 2

Introduction 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. Chapter 7: Expressions and Assignment Statements 3

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. Operators can be: A unary operator has one operand. A binary operator has two operands. A ternary operator has three operands. Chapter 7: Expressions and Assignment Statements 4

Unary Arithmetic Operators in C/C++ and Java Unary Plus ( +A ): Examples: +num, +num/5, num1 * +num2 Has no effect on its operand in C/C++. In Java, it causes an implicit conversion of a char, short, or byte operand to int type; otherwise, it has no effect on its operand. Unary Minus ( -A ): Examples: -num, -num/5, num1 * -num2 Chapter 7: Expressions and Assignment Statements 5

Unary Arithmetic Operators in C/C++ and Java (cont.) The pre-increment is specified as follows: (++a) It says to add 1 to the current value of the variable and then to use the new value (if necessary). The post-increment is specified as follows: (a++) It says to use the current value of the variable (if necessary) and then add 1 to it. The pre-decrement is specified as follows: (--a) It says to subtract 1 from the current value of the variable and then to use the new value (if necessary). The post-decrement is specified as follows: (a--) It says to use the current value of the variable (if necessary) and then subtract 1 from it. Chapter 7: Expressions and Assignment Statements 6

Binary Arithmetic Operators in C/C++ and Java Operation Operator Example Operand Result Addition + A + B Both integers ------------------- One operand is not integer Integer ------------------ Double precision floating-point Subtraction - A – B ---------------------------- ----------------------- Multiplication * A * B ------------------------------ ------------------------- Division / A / B Modulus (Remainder) % A % B Chapter 7: Expressions and Assignment Statements 7

Ternary Operator in C/C++ and Java A ternary operator “ ?: ” has three operands. average = (count==0)? 0 : sum/count; Evaluates as if written like: if (count == 0) average = 0; else average = sum/count; Chapter 7: Expressions and Assignment Statements 8

Arithmetic Expressions: Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated. Typical precedence levels: parentheses. unary operators. ** (the exponential operator, if the language supports it). *, /, % +, - APL has a single level of precedence. Chapter 7: Expressions and Assignment Statements 9

Arithmetic Expressions: 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. APL is different; all operators have equal precedence and all operators associate right to left. Precedence and associativity rules can be overridden with parentheses. Chapter 7: Expressions and Assignment Statements 10

Arithmetic Expressions: Conditional Expressions C-based languages (e.g., C, C++). An example: average = (count == 0)? 0 : sum/count; Evaluates as if written like: if (count == 0) average = 0; else average = sum/count; Chapter 7: Expressions and Assignment Statements 11

Operand Evaluation Order: Functional Side Effects A side effect of a function occurs when a function changes either: one of its parameters or a global variable. Assume fun(x) changes parameter x: y = fun(x) + x; y = x + fun(x); Chapter 7: Expressions and Assignment Statements 12

Functional Side Effect: Example (1 of 2) int a = 5; int fun1() { a = 17; return 3; } void main() { a = a + fun1(); cout << a << endl; } // 8 is printed Chapter 7: Expressions and Assignment Statements 13

Functional Side Effect: Example (2 of 2) int a = 5; int fun1() { a = 17; return 3; } void main() { a = fun1() + a; cout << a << endl; } // 20 is printed Chapter 7: Expressions and Assignment Statements 14

Overloaded Operators Operator Overloading is accomplished by defining functions that have the same name as the operator being overloaded. Some are common (e.g., +, -, *, … for int and float). C++, FORTRAN 95 and Ada allow user-defined overloaded operators. Java does not permit operator overloading. Chapter 7: Expressions and Assignment Statements 15

Type Conversions Type conversions are either: 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. May lose data. 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. Generally safe. Chapter 7: Expressions and Assignment Statements 16

Type Conversions (cont.) Type conversions may be: Implicit (coercion). Explicit (such as casting in C). A mixed-mode expression is one that has operands of different types. A coercion is an implicit type conversion. In most languages, all numeric types are coerced in expressions, using widening conversions. In Java, byte and short are coerced to int whenever an operation is applied. Chapter 7: Expressions and Assignment Statements 17

Explicit Type Conversions Called casting in C-based language. Examples: C conversion (cast) int a, b; float x; x = (float)a/(float)b; Chapter 7: Expressions and Assignment Statements 18

Type Conversions: Errors in Expressions Causes: Errors resulting from narrowing conversions. Errors resulting from limitations of computer arithmetic: Overflow. Underflow. Some languages (Ada, C++, Java) provide an exception handling mechanism that allows the programmer to handle conditions such as divide by 0. Often ignored by the run-time system. Chapter 7: Expressions and Assignment Statements 19

Relational and Boolean Expressions Relational operators in various languages: Ada: =, /=, >, <, >=, <= Java: ==, !=, >, <, >=, <= Relational operators always have lower precedence than arithmetic operators. Relational Expressions: Use relational operators and operands of various types. Evaluate to some Boolean representation. Boolean Expressions: Operands are Boolean and the result is Boolean. C/C++ operators: &&, ||, ! Chapter 7: Expressions and Assignment Statements 20

Relational and Boolean Expressions: No Boolean Type in C C has no Boolean type, it uses int type with 0 for false and nonzero for true. Chapter 7: Expressions and Assignment Statements 21

Short-Circuit Evaluation A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators. Types: In Arithmetic Expressions: Ex.: (13*a) * (b/13–1) If a is zero, there is no need to evaluate (b/13-1). In logical Expressions: Ex.: The value of (a >= 0) and (b < 10) is independent of the second relational expression if a < 0. Chapter 7: Expressions and Assignment Statements 22

Short-Circuit Evaluation (cont.) Lack of short circuit evaluation can cause problems: Index = 1; while ((Index < listlen) && (list[Index] != key)) Index = Index + 1; If the expression doesn’t have short-circuit, then both operands to “&&” are evaluated. C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||). Chapter 7: Expressions and Assignment Statements 23

Short-Circuit Evaluation (cont.) Short-circuit evaluation exposes the problem of side effects in expressions: if ((a > b) || (b++/3)) If (a>b), the second expression which increments b is not evaluated. Ada has short-circuit logical operators: and then and or else if (index <= listlen) and then (list(index) /= key) Chapter 7: Expressions and Assignment Statements 24

Assignment Statements The general syntax: <target_var> <assign_operator> <expression> The assignment operator: = FORTRAN, BASIC, PL/I, C, C++, Java. := ALGOLs, Pascal, Ada. Simple assignment: a = b; a = b = c; Suppose a, b, and c are integers. In C, the integer value of c is assigned to b, which is in turn assigned to a (multiple targets). Chapter 7: Expressions and Assignment Statements 25

Assignment statements Multiple targets: Sum, Total = 0 (PL/I) Sum = Total = 0 (C) Conditional targets: Perl allows it, for example: flag ? count1 : count2 = 0; Which is equivalent to: if (flag) count1 = 0; else count2 = 0; Compound assignment operators: a += b; // a = a + b; Chapter 7: Expressions and Assignment Statements 26

Assignment statements (cont.) Unary assignment operators: count++; ++count; sum = ++count;  count incremented then assigned to sum sum = count++;  count assigned to sum then incremented Assignment as an expression: In C, C++, and Java the assignment statement produces a result and can be used as operands. while ((ch = getchar()) != EOF) { … } ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement. Chapter 7: Expressions and Assignment Statements 27

Mixed-Mode Assignment Assignment statements can also be mixed-mode, for example: int a, b; float c; c = a / b; In Pascal, integer variables can be assigned to real variables, but real variables cannot be assigned to integers In Java, only widening assignment coercions are done. In Ada, there is no assignment coercion.