ISBN 0-321-19362-8 Chapter 7 Expressions and Assignments 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.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
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.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
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.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
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.
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.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
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
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions and Assignment Statements
Expressions & Assignments
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Lecture 3 Expressions Richard Gesick.
Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter-3 Operators.
Expressions and Assignment Statements
Expression and Asignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
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
Presentation transcript:

ISBN Chapter 7 Expressions and Assignments Statements

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

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.

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

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 +, -

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

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.

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 +

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

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;

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)

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

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

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

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.

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 < =

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:

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