Associativity and Prescedence

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
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.
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.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ISBN Chapter 7 Expressions and Assignment Statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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
Operators & Expressions
Expressions and Assignment Statements
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Selections Java.
Computing Fundamentals
CS2403 Programming Languages Expressions and Assignment Statements
University of Central Florida COP 3330 Object Oriented Programming
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Variable Declaration, Data types, Expressions
Expressions and Assignment Statements
Precedence and Associativity
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Expressions and Assignment Statements
Data Types, Identifiers, and Expressions
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Expressions and Assignment Statements
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions and Assignment Statements
Expression and Asignment Statements
Expressions.
Chapter 3 Operators and Expressions
Chapter 7 Expressions and Assignment Statements.
Chapter 4: Expression and Operator
Chapter 7: Expressions and Assignment Statements Sangho Ha
Lexical Elements & Operators
OPERATORS in C Programming
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
OPERATORS in C Programming
Presentation transcript:

Associativity and Prescedence C Programming Associativity and Prescedence

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

OPERATOR PRECEDENCE Precedence and associativity of C operators affect the grouping and evaluation of operands in expressions. Is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first.

Precedence and Associativity of C Operators OPERATOR PRECEDENCE Precedence and Associativity of C Operators Symbol Type of Operation Associativity [ ] ( ) . –> postfix ++ and postfix –– Expression Left to right prefix ++ and prefix –– sizeof &   *   + – ~ ! Unary Right to left typecasts * / % Multiplicative + – Additive << >> Bitwise shift < > <= >= Relational == != Equality & Bitwise-AND ^ Bitwise-exclusive-OR | Bitwise-inclusive-OR && Logical-AND || Logical-OR ? : Conditional-expression = *= /= %=  += –= <<= >>= &= ^= |= Simple and compound assignment , Sequential evaluation www.tenouk.com, ©

OPERATOR PRECEDENCE Operators with equal precedence such as + and -, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right. The direction of evaluation does not affect the results of expressions that include more than one multiplication (*), addition (+), or binary-bitwise (& | ^) operator at the same level. www.tenouk.com, ©

OPERATOR PRECEDENCE e.g: 3 + 5 + (3 + 2) = 13 – right to left Order of operations is not defined by the language. The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result. 3 + 5 + (3 + 2) = 13 – right to left  (3 + 5) + 3 + 2 = 13 – left to right  3 + (5 + 3) + 2 = 13 – from middle www.tenouk.com, ©

OPERATOR PRECEDENCE Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands. The sequential-evaluation operator (,) is guaranteed to evaluate its operands from left to right. The comma operator in a function call is not the same as the sequential-evaluation operator and does not provide any such guarantee. www.tenouk.com, ©

Arithmetic Expressions Arithmetic evaluation was one of the motivations for the development of the first programming languages Arithmetic expressions consist of operators, operands, parentheses, and function calls

Arithmetic Expressions: Operators A unary operator has one operand A binary operator has two operands A ternary operator has three operands

Arithmetic Expressions: Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated Typical precedence levels parentheses unary operators *, / +, -

Precedence and Associativity of Operators Rules of associativity and precedence of operators determine precisely how expressions are operated. In the expression 1 + 2 * 3, the operator * has higher precedence than +, causing the multiplication to be performed first. The result is 7 instead of 9.

Associativity of Operators When two operators placed in proximity in an expression have the same precedence, their associativity is used to determine how the expression is evaluated. In the expression 6 / 2 * 3, both / and * have the same precedence. Since they both have left to right associa-tivity, the expression has the value 9 rather than 1.

Partial Table of Operator Precedence and Associativity Operator Associativity () ++ (postfix) -- (postfix) left to right +(unary) -(unary) ++(prefix) --(prefix) right to left * / % left to right + - left to right = += -= right to left Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Parentheses and the Order of Operations Expressions inside parentheses are evaluated first. This provides for the use of parentheses to clarify or change the order in which operations are performed. 1 + 2 * 3 has a value of 7. (1 + 2)* 3 has a value of 9.

Binary Plus versus Unary Plus Both binary plus and unary plus are represented by a + (plus sign). The same is true of binary and unary - (the minus sign). Unary + and - have a higher precedence that binary + and - and the unary operators associate right-to-left instead of left-to-right.

Example of Unary Operators In the expression - a * b - c the first minus is unary and the second is binary. We can use parentheses to write an equivalent expression that is less likely to be misinterpreted. ((- a) * b) - c

Example of Unary Operators Using Numbers -1 * 2 - 3 has a value of -5 it is equivalent to ((-1) * 2) - 3 or (-2) - 3 which is -5 it is not equivalent to (-1) * (2 - 3) or (-1) * (-1) which is +1

Increment and Decrement Operators ++ (the increment operator) and -- (the decrement operator) are unary operators with the same precedence and right-to-left associativity as the other unary operators. The ++ and -- operators can occur in either a prefix or postfix position with different results.

Prefix versus Postfix When Using Increment and Decrement Operators Each of the expressions ++i and i++ causes the stored value of i to be incremented by 1, however: The expression ++i causes the stored value of i to be incremented first, with the expression then taking as its value the new stored value of i. The expression i++ has as its value the current value of i; then the stored value is incremented.

Example of the Increment and Decrement Operators int a, b, c = 0; a = ++c; b = c++; printf(“%d %d %d\n”, a, b, ++c); /* 1 1 3 is printed */ c is incremented making its value 1. The result assigned to a making its value 1. The value of c is assigned to b making its value 1. Then c is incremented making its value 2. Finally, c is incremented before it is printed, making its value 3.

Practice with Operators and Expressions Declarations and Initializations int a = 1, b = 2, c = 3, d = 4; Expression Equivalent expression Value a * b / c (a * b) / c 0 a * b % c + 1 ((a * b) % c) + 1 3 ++a * b - c -- ((++a) * b) - (c--) 1 7 - -b * ++d 7 - ((-b) * (++d)) 17

Partial Table of Operator Precedence and Associativity Operator Associativity () ++ (postfix) -- (postfix) left to right +(unary) -(unary) ++(prefix) --(prefix) right to left * / % left to right + - left to right = += -= right to left Operators on the top line have the highest precedence. Precedence decreases line-by-line going down the table. All operators on the same line have equal precedence.

Assignment Operators C treats = as an operator. It’s precedence is lower than almost all of the other operators. It’s associativity is right to left.

Form of an Assignment Expression A simple assignment expression is of the form variable = right_side The value of right_side is assigned to variable, and that becomes the value of the assignment expression as a whole.

Assignment Expressions versus Assignment Statements An assignment expression has no semicolon at the end. An assignment statement does. We can use assignment expressions to condense a sequence of assignment statements.

Example of Equivalent Code Using Assignment Expressions Assignment statements b = 2; c = 3; a = b + c; Equivalent statement using assignment expressions a = (b = 2) + (c = 3); Note the assignment statement ends with a semicolon, the expressions don’t.

Other Assignment Operators C has operators that combine assignment with other operations. These are considered assignment operators and have the same precedence and right-to-left associativity as =. Example k = k + 2; is equivalent to k += 2;

The Assignment Operators = += -= *= /= %= >>= <<= &= ^= |= The semantics of the other assignment operators is specified by variable op= expression which is equivalent to variable = variable op (expression) j *= k + 3 is equivalent to j = j * (k + 3) and not j = j * k + 3

Style Do not condense code just for the sake of using less space z = 3; x = y + z; is more readable than x = (y = 2) + (z = 3); and a += 7; versus a = a + 7; is a matter of choice.

Common Programming Errors Warning and Error Messages usually refer to a line number. The problem may be prior to that line -- such as not properly closing a comment or a string constant.

OPERATOR PRECEDENCE Logical operators also guarantee evaluation of their operands from left to right. But, they evaluate the smallest number of operands needed to determine the result of the expression. This is called "short-circuit" evaluation. Thus, some operands of the expression may not be evaluated. www.tenouk.com, ©

OPERATOR PRECEDENCE x && y++ For example:   The second operand, y++, is evaluated only if x is true (nonzero). Thus, y is not incremented if x is false (0). x && y++ www.tenouk.com, ©

System Considerations ANSI C has both a unary + and a unary - Traditional C has only unary -, so you should not use the unary + operator if your are writing code that needs to be portable to a machine that uses traditional C. If you are writing code for a spectrum of systems, limitations of all of the systems must be respected.

Label the execution order for the following expressions OPERATOR PRECEDENCE Label the execution order for the following expressions www.tenouk.com, ©

Convert the following operations to C expression OPERATOR PRECEDENCE Convert the following operations to C expression (rate*rate) + delta 2*(salary + bonus) 1/(time + (3*mass)) (a - 7) / (t + (9 * v)) www.tenouk.com, ©