CIS217 Concepts of Programming Languages Chapter 6 PDF
Document Details
Uploaded by AccomplishedUniverse8955
Tags
Summary
This document is a chapter from a course on programming languages, focusing on expressions and assignment statements. It covers fundamental concepts of programming, including operators and operands, expression evaluation, and different types of expressions. A variety of programming language constructs are described.
Full Transcript
CIS217 CONCEPTS OF PROGRAMMING LANGUAGES CHAPTER-6 Expressions and Assignment Statements Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements...
CIS217 CONCEPTS OF PROGRAMMING LANGUAGES CHAPTER-6 Expressions and Assignment Statements Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment 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. Essence of imperative languages is dominant role of assignment statements. Arithmetic Expressions Their evaluation was one of the motivations for the development of the first programming languages. Most of the characteristics of arithmetic expressions in programming languages were inherited from conventions that had evolved in math. Arithmetic expressions consist of operators, operands, parentheses, and function calls. An operator can be unary, meaning it has a single operand, binary, meaning it has two operands, or ternary, meaning it has three operands. An implementation of such a computation must cause two actions: – Fetching the operands from memory – Executing the arithmetic operations on those operands. Design issues for arithmetic expressions: 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? Operator Evaluation Order 1. Precedence – The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated (“adjacent” means they are separated by at most one operand). – Typical precedence levels: 1. parentheses 2. unary operators 3. ** (if the language supports it) 4. *, / 5. +, - Operator Evaluation Order Many languages also include unary versions of addition and subtraction. Unary addition (+) is called the identity operator because it usually has no associated operation and thus has no effect on its operand. Unary minus operator (-) Ex: Operator Evaluation Order 2. Associativity – The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated. An operator can be either left or right associative. Typical associativity rules: – Left to right, except **, which is right to left – Sometimes unary operators associate right to left (e.g., FORTRAN) Operator Evaluation Order NOTE: Precedence and associativity rules can be overridden with parentheses Operator Evaluation Order 3. Parentheses – Programmers can alter the precedence and associativity rules by placing parentheses in expressions. A parenthesized part of an expression has precedence over its adjacent un-parenthesized parts. Operator Evaluation Order 4. Conditional Expressions – Sometimes if-then-else statements are used to perform a conditional expression assignment. if (count == 0) average = 0; else average = sum / count; In the C-based languages, this can be specified more conveniently in an assignment statement using a conditional expressions. Note that ? is used in conditional expression as a ternary operator (3 operands). expression_1 ? expression_2 : expression_3 Ex: average = (count == 0) ? 0 : sum / count; Operand evaluation order The process: 1. Variables: just 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. Side Effects – A side effect of a function, called a functional side effect, occurs when the function changes either one of its parameters or a global variable. Ex: – If fun does not have the side effect of changing a, then the order of evaluation of the two operands, a and fun(a), has no effect on the value of the expression. Operand evaluation order However, if fun changes a, there is an effect. Ex: – Consider the following situation: fun returns the value of its argument divided by 2 and changes its parameter to have the value 20, and: – If the value of a is returned first (in the expression evaluation process), its value is 10 and the value of the expression is 15. – But if the second is evaluated first, then the value of the first operand is 20 and the value of the expression is 25. Solution: Two possible solutions: 1. Write the language definition to disallow functional side effects No two-way parameters in functions. No non-local references in functions. Disadvantage: Programmers want the flexibility of two-way parameters (what about C?) and non-local references. 2. Write the language definition to demand that operand evaluation order be fixed Overloaded Operator The use of an operator for more than one purpose is operator overloading. Some are common (e.g., + for int and float). Java uses + for addition and for string catenation. Some are potential trouble (e.g., & in C and C++) Causes the address of y to be placed in x. – Some loss of readability to use the same symbol for two completely unrelated operations. Can be avoided by introduction of new symbols (e.g., Pascal’s div for integer division and / for floating point division) 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., double to float. 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. Coercion in Expressions A mixed-mode expression is one that has operands of different types. – A coercion is an implicit type conversion. – The disadvantage of coercions: They decrease in the type error detection ability of the compiler – In most languages, all numeric types are coerced in expressions, using widening conversions Language are not in agreement on the issue of coercions in arithmetic expressions. Type Conversions Explicit Type Conversions – Often called casts in C-based languages. – Ex: Ada: FLOAT(INDEX)--INDEX is INTEGER type Java: (int)speed Errors in Expressions – Caused by: Inherent limitations of arithmetic e.g. division by zero Limitations of computer arithmetic e.g. overflow or underflow – Floating-point overflow and underflow, and division by zero are examples of run-time errors, which are sometimes called exceptions. Relational and Boolean Expressions A relational operator: an operator that compares the values of its tow operands. Relational Expressions: two operands and one relational operator. The value of a relational expression is Boolean, unless it is not a type included in the language. – Use relational operators and operands of various types. – Operator symbols used vary somewhat among languages (!=, /=,.NE., , #) The syntax of the relational operators available in some common languages Relational and Boolean Expressions Boolean Expressions: – Operands are Boolean and the result is Boolean. Versions of C prior to C99 have no Boolean type; it uses int type with 0 for false and nonzero for true. One odd characteristic of C’s expressions: – a < b < c is a legal expression, but the result is not what you might expect. The left most operator is evaluated first because the relational operators of C, are left associative, producing either 0 or 1. Then this result is compared with var c. There is never a comparison between b and c. Assignment Statements Simple Assignments – The C-based languages use == as the equality relational operator to avoid confusion with their assignment operator. The operator symbol for assignment: 1. = FORTRAN, BASIC, PL/I, C, C++, Java 2. := ALGOL, Pascal, Ada Conditional Targets – Ex: Compound Assignment Operators A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment. The form of assignment that can be abbreviated with this technique has the destination var also appearing as the first operand in the expression on the right side, as in The syntax of assignment operators that is the catenation of the desired binary operator to the = operator. Unary Assignment Operators C-based languages include two special unary operators that are actually abbreviated assignments. They combine increment and decrement operations with assignments. The operators ++ and -- can be used either in expression or to form standalone single-operator assignment statements. They can appear as prefix operators: If the same operator is used as a postfix operator: Assignment as an Expression This design treats the assignment operator much like any other binary operator, except that it has the side effect of changing its left operand. – Ex: The assignment statement must be parenthesized because the precedence of the assignment operator is lower than that of the relational operators. – Disadvantage: Another kind of expression side effect which leads to expressions that are difficult to read and understand. For example Mixed-Mode Assignment In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable; whatever conversion is necessary is done. In Pascal, integers can be assigned to reals, but reals cannot be assigned to integers (the programmer must specify whether the conversion from real to integer is truncated or rounded.) In Java, only widening assignment coercions are done. In Ada, there is no assignment coercion. In all languages that allow mixed-mode assignment, the coercion takes place only after the right side expression has been evaluated. For example= int a, b; float c; … c = a / b; Because c is float, the values of a and b could be coerced to float before the division, which could produce a different value for c than if the coercion were delayed (for example, if a were 2 and b were 3).