Operators and Expressions

Slides:



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

All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
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.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Operaciones y Variables
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.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Operators and Expressions. Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Operators & Expressions
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
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.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Rational Expressions relational operators logical operators order of precedence.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
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
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
BIL 104E Introduction to Scientific and Engineering Computing
Selections Java.
Tokens in C Keywords Identifiers Constants
Relational Operations
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 and Assignment Statements
Expressions and Assignment Statements
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
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 3 Operators and Expressions
Chapter 7 Expressions and Assignment Statements.
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator King Saud University
Expressions and Assignment Statements
Lecture 9: Implementing Complex Logic
Introduction to Python
Presentation transcript:

Operators and Expressions Session 3 Operators and Expression

Objectives Explain Assignment Operator Understand Arithmetic Expressions Explain Relational and Logical Operators Understand Bitwise logical operators and expressions Explain casts Understand Precedence of Operators Elementary Programming with C/Session 3/ 2 of 25

Expressions Combination of Operators and Operands Operators Operands Example 2 * y + 5 Operators Operands Elementary Programming with C/Session 3/ 3 of 25

variable_name = expression; The Assignment Operator The assignment operator(=) can be used with any valid C expression variable_name = expression; lvalue rvalue Assignment operator (Left value) (Right value) Elementary Programming with C/Session 3/ 4 of 25

 X Multiple Assignment a = b = c = 10; Many variables can be assigned the same value in a single statement  a = b = c = 10; However, you cannot do this : X int a = int b = int b = int c = 10 Elementary Programming with C/Session 3/ 5 of 25

Operators 4 Types Arithmetic Logical Relational Bitwise Elementary Programming with C/Session 3/ 6 of 25

Arithmetic Expressions Mathematical expressions can be expressed in C using arithmetic operators Examples ++i % 7 5 + (c = 3 + 8) a * (b + c/d)22 Elementary Programming with C/Session 3/ 7 of 25

Relational & Logical Operators-1 Used to…… Test the relationship between two variables, or between a variable and a constant Relational Operators Elementary Programming with C/Session 3/ 8 of 25

Relational & Logical Operators-2 Logical operators are symbols that are used to combine or negate expressions containing relational operators Example: if (a>10) && (a<20) Expressions that use logical operators return zero for false, and 1 for true Elementary Programming with C/Session 3/ 9 of 25

Bitwise Logical Operators-1 Processes data after converting number to its binary equivalent. (Bit wise representation) AND ( NUM1 & NUM2) Return 1 if both the operands are 1 OR ( NUM1 | NUM2 ) Returns 1 if bits of either of the operand are 1 NOT ( ~ NUM1) Reverses the bits of its operand ( from 0 to 1 and 1 to 0) XOR ( NUM1 ^ NUM2) Returns 1 if either of the bits in an operand is 1 but not both Elementary Programming with C/Session 3/ 10 of 25

Bitwise Logical Operators-2 Example 10 & 15 1010 & 11111010  10 10 | 15 1010 | 11111111  15 10 ^ 15 1010 ^ 11110101  5 ~ 10  ~1010 1011  -11 Elementary Programming with C/Session 3/ 11 of 25

Type Conversion Example Elementary Programming with C/Session 3/ 12 of 25

Casts An expression can be forced to be of a certain type by using a cast. The general syntax of cast: (type) cast type  any valid C data type The integer value returned by (int)f is converted back to floating point when it crossed the assignment operator. The value of f itself is not changed. Example: float x,f; f = 3.14159; x = (int) f; , the value of x will be 3 (integer) Elementary Programming with C/Session 3/ 13 of 25

Precedence Of Operators-1 Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression is to be evaluated It refers to the order in which C evaluates operators The precedence of the operators can be altered by enclosing the expressions in parentheses Operator Class Operators Associativity Unary - ++ -- Right to Left Binary ^ Left to Right * / % + - = Elementary Programming with C/Session 3/ 14 of 25

Precedence Of Operators-2 Example : -8 * 4 % 2 - 3 Elementary Programming with C/Session 3/ 15 of 25

Precedence between comparison Operators Always evaluated from left to right Elementary Programming with C/Session 3/ 16 of 25

Precedence for Logical Operators-1 Precedence Operator 1 NOT 2 AND 3 OR When multiple instances of a logical operator are used in a condition, they are evaluated from right to left Elementary Programming with C/Session 3/ 17 of 25

Precedence for Logical Consider the following expression Operators-2 Consider the following expression False OR True AND NOT False AND True This condition gets evaluated as shown below: False OR True AND [NOT False] AND True NOT has the highest precedence. False OR True AND [True AND True] AND is the operator of the highest precedence and operators of the same precedence are evaluated from right to left False OR [True AND True] [False OR True] True Elementary Programming with C/Session 3/ 18 of 25

Precedence among Operators-1 When an equation uses more than one type of operator then the order of precedence has to be established with the different types of operators Precedence Type of Operator 1 Arithmetic 2 Comparison 3 Logical Elementary Programming with C/Session 3/ 19 of 25

Precedence among Operators-2 Consider the following example: 2*3+4/2 > 3 AND 3<5 OR 10<9 The evaluation is as shown: [2*3+4/2] > 3 AND 3<5 OR 10<9 First the arithmetic operators are dealt with [[2*3]+[4/2]] > 3 AND 3<5 OR 10<9 [6+2] >3 AND 3<5 OR 10<9 [8 >3] AND [3<5] OR [10<9] Elementary Programming with C/Session 3/ 20 of 25

Precedence among Operators-3 Next to be evaluated are the comparison operators all of which have the same precedence and so are evaluated from left to right True AND True OR False The last to be evaluated are the logical operators. AND takes precedence over OR [True AND True] OR False True OR False True Elementary Programming with C/Session 3/ 21 of 25

Changing Precedence-1 Parenthesis ( ) has the highest level of precedence The precedence of operators can be modified using parenthesis ( ) Operator of lower precedence with parenthesis assume highest precedence and gets executed first In case of nested Parenthesis ( ( ( ) ) ) the inner most parenthesis gets evaluated first An expression consisting of many set of parenthesis gets processed from left to right Elementary Programming with C/Session 3/ 22 of 25

Changing Precedence-2 Consider the following example: 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (2<6 AND 10>11)) The solution is: 1. 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (True AND False)) The inner parenthesis takes precedence over all other operators and the evaluation within this is as per the regular conventions 2. 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR False) Elementary Programming with C/Session 3/ 23 of 25

Changing Precedence-3 3. 5+9*3^2-4 >10 AND (2+16-8/4 > 6 OR False) Next the outer parentheses is evaluated 4. 5+9*3^2-4 > 10 AND (2+16-2 > 6 OR False) 5. 5+9*3^2-4 > 10 AND (18-2 > 6 OR False) 6. 5+9*3^2-4 > 10 AND (16 > 6 OR False) 7. 5+9*3^2-4 > 10 AND (True OR False) 8. 5+9*3^2-4 > 10 AND True Elementary Programming with C/Session 3/ 24 of 25

Changing Precedence-4 9. 5+9*9-4>10 AND True The expression to the left is evaluated as per the conventions 10. 5+81-4>10 AND True 11. 86-4>10 AND True 12. 82>10 AND True 13. True AND True 14. True Elementary Programming with C/Session 3/ 25 of 25