1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Chapter 7 data type expressions and assignment statements

13 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 13
Dung lượng 99,9 KB

Nội dung

Chapter Data Type Expressions and Assignment Statements ISBN 0-321-33025-0 Chapter Topics • Introduction • Arithmetic Expressions • Overloaded Operators • Type Conversions • Relational R l i l and dB Boolean l E Expressions i • Short-Circuit Evaluation • Assignment Statements • Mixed-Mode Assignment Copyright © 2006 Addison-Wesley All rights reserved 1-2 Introduction • Expressions are the fundamental means of specifying computations in a programming language • To understand expression evaluation, need to be familiar with the following concepts: – The orders of operator and operand evaluation – Type mismatch – Coercion • Essence of imperative languages is dominant role of assignment statements Copyright © 2006 Addison-Wesley All rights reserved 1-3 Arithmetic Expressions • Their evaluation was one of the motivations for the development of the first programming languages • Arithmetic expressions consist of operators, operands, parentheses, and function calls Copyright © 2006 Addison-Wesley All rights reserved 1-4 Arithmetic Expressions – Design issues • What are the operator precedence rules? • What are the operator associativity rules? • What is the order of operand evaluation? • Are there restrictions on operand evaluation side effects? • Does the language allow user-defined operator overloading? • What mode mixing is allowed in expressions? Copyright © 2006 Addison-Wesley All rights reserved 1-5 Arithmetic Expressions - Operators • A unary operator has one operand • A binary operator has two operands • A ternary operator has three operands Copyright © 2006 Addison-Wesley All rights reserved 1-6 Arithmetic Expressions – Operator Precedence • The operator precedence rules for expression evaluation define the order in which “adjacent” adjacent operators of different precedence levels are evaluated – “Adjacent” means they are separated by at most one operand • Typical precedence levels 2 Parentheses Unary operators ** (exponentiation, if the language supports it) *, / +, - Copyright © 2006 Addison-Wesley All rights reserved 1-7 Arithmetic Expressions – Operator Associativity • 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, g except p **, which is right g to left – Example: In FORTRAN A**B**C  A**(B**C) • APL: all operators have equal precedence and all operators associate right to left • Precedence P d and d associativity i ti it rules l can b be overriden with parentheses Copyright © 2006 Addison-Wesley All rights reserved 1-8 Operand evaluation order • Variables in expressions are evaluated by fetching their values from memory • If an operand is a parenthesized expression, then all operators it contains must be evaluated before its value can be used as an operand • If neither of the operands of an operator has side effects, then operand evaluation order is irrelevant Copyright © 2006 Addison-Wesley All rights reserved 1-9 Functional side effects • A side effect of a function occurs when the function changes either one of its two-way parameters or a nonlocal variable • The problem with functional side effects: – When a function referenced in an expression alters another operand of the expression Copyright © 2006 Addison-Wesley All rights reserved 1-10 Example a = 10; b = a + fun(a); … int fun(int &a) { int b = a / 2; a *= 2; return b; } int a = 5; int fun1() { a = 17; return 3; } void id fun2() f 2() { a = a + fun1(); } void main() { fun2(); (); } Copyright © 2006 Addison-Wesley All rights reserved 1-11 Functional side effects - Solutions Write the language definition to disallow functional side effects – No two-way parameters in functions – No nonlocal references in functions – Advantage: it works! – Disadvantage: Programmers want the flexibility of two-way parameters and nonlocal references Write the language definition to demand that operand evaluation order be fixed – Disadvantage: limits some compiler optimizations Copyright © 2006 Addison-Wesley All rights reserved 1-12 Overloaded Operators • Use of an operator for more than one purpose is called operator overloading • Some are common (e.g., + for int and float) potential trouble ((e.g., g , * in C/C++) / ) • Some are p – Loss of compiler error detection (omission of an operand should be a detectable error) – Readability may suffer suffer, even when the operators make sense A * B + C * D >< MatAdd(MatMult(A, B), MatMult(C, D)) but nothing prevents a user from defining + to mean addition of elements in array • Can be avoided by introduction of new symbols (e.g., Pascal’s div) Copyright © 2006 Addison-Wesley All rights reserved 1-13 Type Conversions • 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 i converted is t d tto a ttype th thatt can iinclude l d att lleastt approximations to all of the values of the original type, e.g., int to float Copyright © 2006 Addison-Wesley All rights reserved 1-14 Coercion in Expressions • A mixed-mode expression is one that has operands of different types • A coercion is an implicit type conversion – Disadvantage: They decrease in the type error detection ability of the compiler • In most languages, all numeric types are coerced in expressions, using widening conversions • In Ada, there are virtually no coercions in expressions Copyright © 2006 Addison-Wesley All rights reserved 1-15 Explicit Type Conversion - Cast • Most languages provide some capability for doing explicit conversions, both widening and narrowing • In some cases, warning messages are produced when h an explicit li it narrowing i conversion i results lt iin a significant change to the value of the object being converted Copyright © 2006 Addison-Wesley All rights reserved 1-16 Relational and Boolean Expressions • Relational Expressions – Use relational operators and operands of various types – Operator symbols used vary somewhat among languages (!=, /=, NE., , #) • Boolean Expressions – Boolean expressions p consist of Boolean variables,, Boolean constants, relational expressions, and Boolean operators Copyright © 2006 Addison-Wesley All rights reserved 1-17 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 • Example: index = 1; while (index < listlen) && (list[index] != key) index++; Copyright © 2006 Addison-Wesley All rights reserved 1-18 Short Circuit Evaluation - Languages • C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuit (& and |) • Short-circuit Sh t i it evaluation l ti exposes th the potential t ti l problem of side effects in expressions e.g (a > b) || (b++ / 3) Copyright © 2006 Addison-Wesley All rights reserved 1-19 Short Circuit Evaluation - Languages • Ada: programmer can specify either (short- circuit is specified with and then and or else) INDEX := 1; while (INDEX b) or else (b++ / 3); Copyright © 2006 Addison-Wesley All rights reserved 1-20 Assignment Statements • The assignment statement is one of the central constructs in imperative languages • It provides the mechanism by which the user can dynamically change the bindings of values to variables i bl Copyright © 2006 Addison-Wesley All rights reserved 1-21 Simple Assignments • • The operator symbol: – FORTRAN, FORTRAN BASIC, BASIC PL/I, PL/I C, C C++, C++ Java: ‘=’ – ALGOLs, Pascal, Ada: ‘:=’ • ‘=‘ can be bad if it is overloaded for the relational operator for equality e.g (PL/I) A = B = C; Copyright © 2006 Addison-Wesley All rights reserved 1-22 More complicated assignments • Multiple targets (PL/I) A, B = 10 • Conditional targets (C, C++, and Java) (first == true) ? total : subtotal = • Compound assignment operators (C/C++, Java) sum += next; • C, C++, and Java treat = as an arithmetic binary operator t a = b * (c = d * + 1) + a = b + ( (c = d / b++) – 1; (side effect) Copyright © 2006 Addison-Wesley All rights reserved 1-23 More complicated assignments (cont.) • Assignment as an Expression – In C, C++, and Java, the assignment statement produces a result p – So, they can be used as operands in expressions e.g while ((ch = getchar())!= EOF) { … } Copyright © 2006 Addison-Wesley All rights reserved 1-24 Mixed-Mode Assignment • In FORTRAN, C/C++, any numeric value can be assigned i d tto any numeric i scalar l variable; i bl whatever conversion is necessary is done • In Pascal, l integers can b be assigned d to reals, l b but reals cannot be assigned to integers (the user must specify if whether h h the h conversion i from f reall to integer is truncated or rounded) • In Java, only widening assignment coercions are done • In Ada, there is no assignment coercion Copyright © 2006 Addison-Wesley All rights reserved 1-25 ... Addison-Wesley All rights reserved 1-16 Relational and Boolean Expressions • Relational Expressions – Use relational operators and operands of various types – Operator symbols used vary somewhat among... More complicated assignments (cont.) • Assignment as an Expression – In C, C++, and Java, the assignment statement produces a result p – So, they can be used as operands in expressions e.g while... following concepts: – The orders of operator and operand evaluation – Type mismatch – Coercion • Essence of imperative languages is dominant role of assignment statements Copyright © 2006 Addison-Wesley

Ngày đăng: 23/03/2022, 08:31

w