22
Chapter 7 Expressions and Assignment Statements

Chapter 7

  • Upload
    alyssa

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 7. Expressions and Assignment Statements. Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter  7

Chapter 7

Expressions and Assignment Statements

Page 2: Chapter  7

2

IntroductionArithmetic ExpressionsOverloaded OperatorsType ConversionsRelational and Boolean ExpressionsShort-Circuit EvaluationAssignment StatementsMixed-Mode Assignment

Page 3: Chapter  7

3

- Expressions are the fundamental means of specifying computations in a programming language- To understand expression evaluation, we need to be familiar with the orders of operator and operand evaluation- The essence of imperative languages is the dominant role of assignment statements

Page 4: Chapter  7

IntroductionIssues

Order of evaluationType mismatch and coercionsShort circuit evaluation

Assignment statementUsed in imperative languages to change the

value of a variableContains an expression to be evaluated and a

target location for the answer.

4

Page 5: Chapter  7

Arithmetic Expressions Most of the characteristics of arithmetic

expressions in programming languages were inherited from conventions that had evolved in mathematics.

The purpose of an arithmetic expression is to specify an arithmetic computation. The two actions of this are: fetching the operands, usually from memory, executing the arithmetic operations on those

operands In programming languages, arithmetic

expressions consist of operators, operands, parentheses, and function calls.

The operators can be unary, meaning they have a single operand, or binary, meaning they have two operands.

C, C++, and Java include a ternary operator, which has three operands.

5

Page 6: Chapter  7

Arithmetic Expressions (continued)Design issues for arithmetic expressions

What are the operator precedence (priority) 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?

6

Page 7: Chapter  7

Arithmetic Expressions (continued)Number of operands for an operator

A unary operator has one operandA binary operator has two operandsA ternary operator has three operands

The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated.Typical precedence levelsparenthesesunary operators** (if the language supports it)*, /, %+, -

7

Page 8: Chapter  7

Arithmetic Expressions (continued)The order of an expression depends on the order

of evaluation of the operators

The operator precedence rules for expression evaluation define the order in which operators of different precedence levels are evaluated. These rules are based on the hierarchy of operator priorities.

Unary addition is called identity operator because it usually has no associated operation and thus has no effect on its operand.

8

Page 9: Chapter  7

Arithmetic Expressions (continued)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 leftSometimes unary operators associate right to left (e.g.,

FORTRAN)APL is different; all operators have equal precedence and

all operators associate right to leftPrecedence and associativity rules can be overriden with

parenthesesOperand evaluation order

The process:Variables: just fetch the valueConstants: sometimes a fetch from memory;

sometimes the constant is in the machine language instruction

Parenthesized expressions: evaluate all operands and operators first

Function references: The case of most interest!Order of evaluation is crucial

9

Page 10: Chapter  7

Arithmetic Expressions (continued)• Functional side effects - when a function

changes a two-way parameter or a nonlocal variable

• The problem with functional side effects: – When a function referenced in an expression

alters another operand of the expression e.g., for a parameter change: a = 10;b = a + fun(&a); /* Assume that fun changes its parameter */

– Same problem with global variables

10

Page 11: Chapter  7

Arithmetic Expressions (continued)Two Possible Solutions to the Problem:

Write the language definition to disallow functional side effectsNo two-way parameters in functionsNo nonlocal references in functionsAdvantage: it works!Disadvantage: Programmers want the flexibility of two-way

parameters (what about Java?) and nonlocal referencesWrite the language definition to demand that

operand evaluation order be fixedDisadvantage: limits some compiler optimizations

Conditional ExpressionsC, C++, and Java (?:) e.g.

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

11

Page 12: Chapter  7

Overloaded OperatorsOperator overloading is the multiple use of

operators. Example, the “+” is used for addition and for

string concatenation in Java. The “&’’ in C is a problem and overloading the –

and + operator is also a problem.Loss of compilerLoss of readabilityCan be avoided by introducing new symbols

C++ and Ada allow user-defined overloaded operatorsPotential problem if user defines ”nonsense” operators

like using + to mean multiply12

Page 13: Chapter  7

Type ConversionsA 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 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

A mixed-mode expression is one that has operands of different types

A coercion is an implicit type conversion, either during compilation or during run time. A typical example would be an expression mixing integer and floating point numbers (like 5 + 0.1), where the integers are normally converted into the latter

The disadvantage of coercions:They decrease the type error detection ability of the

compiler In most languages, all numeric types are coerced in

expressions, using widening conversionsIn Ada, there are virtually no coercions in

expressions

13

Page 14: Chapter  7

Type Conversions (cont.)Explicit Type Conversions

Often called casts e.g. Ada:

FLOAT(INDEX) -- INDEX is INTEGER typeJava:

(int)speed /* speed is float type */Errors in Expressions

Caused by:Inherent limitations of arithmetic e.g. division by zeroLimitations of computer arithmetic e.g. overflow

Such errors are often ignored by the run-time system

14

Page 15: Chapter  7

Relational and Boolean ExpressionsRelational Expressions:

Use relational operators and operands of various types

Evaluate to some Boolean representationOperator symbols used vary somewhat among

languages (!=, /=, .NE., <>, #)Boolean Expressions:

Operands are Boolean and the result is BooleanOperators:

FORTRAN 90 Java C Adaand & && and

or | || ornot ! ! not ^ xor

C has 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 expect15

Page 16: Chapter  7

Relational and Boolean Expressions (continued)The precedence of the arithmetic, relational, and

Boolean operators in the C-based languages is as follows:

postfix ++, --unary +, -, prefix ++, --, !*, /, %binary +, -<, >, <=, >===, !=&&||

C, C++, and Java have over 40 operators and least 14 different levels of precedence

16

Page 17: Chapter  7

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.

• Ex., (13 * A) * (B / 13 – 1) is independent of the value (B / 13 – 1) if a is 0. Because 0*x = 0 for any x. So if A is 0 there is no need to evaluate (B / 13 – 1).

• Short-circuit evaluation of expressions exposes the problem of allowing side effects in expressions.

• C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||).

• Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)17

Page 18: Chapter  7

Assignment Statements

The operator symbol:= FORTRAN, BASIC, PL/I, C, C++, Java:= ALGOLs, Pascal, Ada= Can be bad if it is overloaded for the relational

operator for equalitye.g. (PL/I) A = B = C; A is set to the Boolean value for

the result of B = CIn the C statement A = B = C, the value of C is assigned

to B, and the value of B is assigned to A.

18

Page 19: Chapter  7

Assignment Statements (continued)More complicated assignments:

Multiple targets (PL/I)A, B = 10

Conditional targets (C, C++, and Java) if(flag) count1 = 0; else count2 = 0;

Compound assignment operators (C, C++, and Java)sum += next;

Unary assignment operators (C, C++, and Java)a++;

C, C++, and Java treat = as an arithmetic binary operatore.g.

a = b * (c = d * 2 + 1) + 1This is inherited from ALGOL 68

19

Page 20: Chapter  7

Assignment Statements (continued)

Assignment as an ExpressionIn C, C++, and Java, the assignment statement

produces a result So, they can be used as operands in expressions

e.g. while ((ch = getchar()) != EOF) { ... }Disadvantage

Another kind of expression side effect of changing its left operand.

20

Page 21: Chapter  7

Mixed-Mode AssignmentIn 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

21

Page 22: Chapter  7

22

SummaryExpressionsOperator precedence and associativityOperator overloadingMixed-type expressionsVarious forms of assignment