Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISBN 0-321-19362-8 Chapter 7 Expressions and Assignments Statements.

Similar presentations


Presentation on theme: "ISBN 0-321-19362-8 Chapter 7 Expressions and Assignments Statements."— Presentation transcript:

1 ISBN 0-321-19362-8 Chapter 7 Expressions and Assignments Statements

2 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 7 Topics Introduction Arithmetic Expressions  Arithmetic Expressions Evaluation Rules  Precedence  Association  Parenthesis  Conditional Expressions Overloaded Operators Type Conversions Relational & Boolean Expressions

3 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-3 Introduction The purpose of assignment statements is to change the value of a variable, which is the attribute of imperative language. In many cases assignment statements include expressions with operators, which cause values to be copied to processor and the results to be copied to memory.

4 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-4 Arithmetic Expressions Usually Arithmetic Expressions consist of:  Operators  Operands  Parenthesis  functions

5 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-5 Arithmetic Expressions Evaluation Rules Precedence: the operator precedence rules for expressions are based on the hierarchy of operator priorities: In FORTRAN : Highest ** *, / Lowest +, - Binary +, -

6 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-6 Arithmetic Expressions Evaluation Rules In C: Highest postfix ++, -- prefix ++, -- Lowest *, /, +, -, %

7 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-7 Arithmetic Expressions Evaluation Rules Association: When an expression contains two adjacent occurrences of operators with the same level of precedence association rules of a language decide which operator evaluated first. Association in most languages is left to right except for exponentiation operator its right to left.

8 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-8 Arithmetic Expressions Evaluation Rules In FORTRAN : Left to right *, /, +, - Right to left ** In C : Left to right *, /, %, binary+, binary- Right to left ++, --, unary -, unary +

9 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-9 Arithmetic Expressions Evaluation Rules Parenthesis: we can alter precedence and association rules by placing parenthesis in expressions : (A+B)*C The addition will be evaluated first

10 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-10 Arithmetic Expressions Evaluation Rules Conditional Expressions: if – then – else statements are to perform a conditional expression assignment, in C language more convenient to use the form: Expression1 ? Expression2 : Expression3 It simply states: if expression1 then expression2 else expression3 For example to assign the maximum of x and y to z: z = (x > y)? x : y ; which is the same as: if (x > y) z = x; else z = y;

11 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-11 Overloaded Operators The multiple use of an operator is called operator overloaded and its accepted as long as readability and reliability are not affected. Some multiple use of operators are not detected by the compiler which cause errors, example: x=&y in C it causes the address of y to be placed in x. but ampersand (&) in C we use it as binary operator (AND)

12 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-12 Type Conversions Type conversions are either narrowing or widening. Narrowing conversion converts a value to a type that cannot store even approximations of all the values of the original type. widening conversion converts a value to a type that can include at least approximations of all the values of the original type. Type conversions can be either explicit or implicit

13 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-13 Type Conversions Coerced type conversions: Some languages allow coerced type conversion for mixed mode arithmetic expressions, in C based languages there are smaller types than integer like byte and short.These operands are all coerced to int when any operator is applied to them, then the result is converted to byte or short

14 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-14 Type Conversions Explicit Type Conversion: Most languages provide explicit conversions both widening and narrowing. in C language explicit type conversions are called casts, to specify a cast we use parenthesis before the expression to be converted Example: (int) angle

15 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-15 Relational & Boolean Expressions Relational Expressions:  A relational operator is an operator that compares the values of its two operands.  A relational expression has two operands and one relational operator  The value of a relational expression is Boolean, except when Boolean is not a type included in the language.

16 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-16 Relational & Boolean Expressions The syntax of the relational operators available in some languages as follows: OperationC based languagesFORTRAN 95 Equal=.EQ. or = = Not equal! =.NE. or Greater than>.GT. or > Less than<.LT. or < Greater than or equal> =.GE. or > = Less than or equal< =.LE. or < =

17 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-17 Relational & Boolean Expressions Boolean Expressions:  Boolean Expressions consist of Boolean variables, Boolean constants, relational expressions and operators.  The precedence of the arithmetic, relational and Boolean operators in C languages is:

18 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-18 Relational & Boolean Expressions HighestPostfix ++, -- Unary +,-; Prefix ++, --, ! *, /, % Binary +,-, = =, / = && Lowest//


Download ppt "ISBN 0-321-19362-8 Chapter 7 Expressions and Assignments Statements."

Similar presentations


Ads by Google