CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 2 part #4 Operator
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
OPERATORS.
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(+=,-=,*=……..)
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.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
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.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
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.
Prepared By: K. U. Khimani Assistant Professor IT Department.
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.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
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.
Variables, Operators, and Expressions
Operators & Expressions
Expressions and Assignment Statements
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Operators and Expressions
BIL 104E Introduction to Scientific and Engineering Computing
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.
Assignment and Arithmetic expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Chapter 12 Variables and Operators
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
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 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Chapter 12 Variables and Operators
Presentation transcript:

CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND

=> OPERATORS: “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables” =>The objects of the operation(s) are referred to as Operands. Ex: a+b operands Operator

Arithmetic Operators =>Unary Operator: It act on one operand. => Unary +: The operator unary ‘+’ precedes an operand. Example: if a=5 then +a means 5. => Unary -: The operator unary ‘-’ precedes an operand. Example: if a=5 then -a means -5.

Arithmetic Operators =>Arithmetic operators +, -, * are the same as in math =>Division operator / if used on integers returns integer (without rounding) or exception =>Division operator / if used on real numbers returns real number or Infinity or NaN =>Remainder operator % returns the remainder from division of integers. If nominator is less than denominator then remainder is always the nominator value. => Binary Operator: It act upon two operands.

Arithmetic operators OperatorDescriptionExample +Adds two operands A + B will give 30,if A=20 and B=10 - Subtracts second operand from the first A - B will give -10 *Multiply both operandsA * B will give 200 / Divide numerator by de- numerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increment operatorIncrement operator, increases integer value by one A++ will give Decrement operatorDecrement operator, decreases integer value by one A-- will give 9

Increment(++) & Decrement(– –) Operators C++ supports 2 useful operators namely 1.Increment ++ 2.Decrement – – operators =>The ++ operator adds a value 1 to the operand =>The – – operator subtracts 1 from the operand ++a //pre-increment a++ //post-increment – –a //pre- Decrement a– – //post- Decrement

Rules for ++ & -- operators 1.These require variables as their operands 2.The postfix either ++ or – – operators follow use-then-change rule i.e., they first use the value of their operand in evaluating the expression and then change(++/ – – ) the operand value. 3.The prefix either ++ or – – operators follow change-then-use rule i.e., they first change(++/ – – ) the value of their operand, then use the new value in evaluating the expression. Note: In Turbo C++, First all prefix operators are evaluated prior to expression evaluation.

Examples for ++ & -- operators Let the value of a =5 and b=++a then a = b =6 Let the value of a = 5 and b=a++ then a =6 but b=5 i.e.: 1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 2. a postfix operator first assigns the value to the variable on left and then increments the operand.

Q.1: Evaluate z=++x + x++ if x=10 initially. Output:22 Q.2.Find the output of the following code segment. int n=7; cout<<“n++=“<<n++<<“,n=“<<n; Output: n++=7,n=8 Q.2.Find the output of the following code segment. int n=7; cout<<“++n=“<<++n<<“,n=“<<n; Output: ++n=8,n=8

Relational Operators: The relational operators determine the relation among different operands. OperatorMeaning <Is less than <=Is less than or equal to >Is greater than >=Is greater than or equal to ==Equal to !=Not equal to

OperatorDescriptionExample == Checks if the value of two operands is equal or not, if equal then condition becomes true. (A == B) is true. != Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Logical Operators: There are following logical operators supported by C++ language OperatorMeaning &&Logical AND ||Logical OR !Logical NOT Logical expression or a compound relational expression: An expression that combines two or more relational expressions Ex: if (a==b && b==c)

operatorDescriptionExample && Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false. If any input values is zero || Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

Truth Table ab Value of the expression a && ba || b

Conditional(Ternary) operators Syntax: exp1 ? exp2 : exp3 Where exp1,exp2 and exp3 are expressions Working of the ? Operator: Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression, If exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression Ex: m=2; n=3 r=(m>n) ? m : n; Output: r=3

Assignment Operators: OperatorDescriptionExample = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A <<=Left shift AND assignment operatorC <<= 2 is same as C = C << 2 >>=Right shift AND assignment operatorC >>= 2 is same as C = C >> 2 &=Bitwise AND assignment operatorC &= 2 is same as C = C & 2 ^=bitwise exclusive OR and assignment operatorC ^= 2 is same as C = C ^ 2 |=bitwise inclusive OR and assignment operatorC |= 2 is same as C = C | 2

Special operators 1.Comma operator (,) 2.sizeof operator – sizeof( ) 3.Pointer operators – ( & and *) 4.Member selection operators – (. and ->)

OperatorDescription sizeof sizeof operatorsizeof operator returns the size of a variable. For example sizeof(a), where a is integer, will return 2., Comma operatorComma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.. (dot) and -> (arrow) Member operatorsMember operators are used to reference individual members of classes, structures, and unions. & Pointer operator &Pointer operator & returns the address of an variable. For example &a; will give actual address of the variable. * Pointer operator *Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.

Precedence of operators: Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator: =>BODMAS RULE: Brackets of Division Multiplication Addition Subtraction Brackets will have the highest precedence and have to be evaluated first, then comes of, then comes division, multiplication, addition and finally subtraction. C++ language uses some rules in evaluating the expressions and they r called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least. The 2 distinct priority levels of arithmetic operators in c++ are- Highest priority : * / % Lowest priority : + -

Rules for evaluation of expression 1.First parenthesized sub expression from left to right are evaluated. 2.If parentheses are nested, the evaluation begins with the innermost sub expression 3.The precedence rule is applied in determining the order of application of operators in evaluating sub expressions 4.The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. 5.Arithmetic expressions are evaluated from left to right using the rules of precedence 6.When parentheses are used, the expressions within parentheses assume highest priority

Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. Category Operator Associativity Postfix () [] -> Left to right Unary + - ! ~ (type)* & sizeofRight to left Multiplicative * / %Left to right Additive + -Left to right Shift >Left to right Relational >=Left to right Equality == !=Left to right Bitwise AND &Left to right Bitwise XOR ^Left to right Bitwise OR |Left to right Logical AND &&Left to right Logical OR ||Left to right Conditional ?:Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |=Right to left Comma,Left to right

Example 1 Evaluate x1=(-b+ sqrt a=1, b=-5, c=6 =(-(-5)+sqrt((-5)(-5)-4*1*6))/(2*1) =(5 + sqrt((-5)(-5)-4*1*6))/(2*1) =(5 + sqrt(25 -4*1*6))/(2*1) =(5 + sqrt(25 -4*6))/(2*1) =(5 + sqrt(25 -24))/(2*1) =(5 + sqrt(1))/(2*1) =( )/(2*1) =(6.0)/(2*1) =6.0/2 = 3.0

Arithmetic Arithmetic Expressions Relational Relational Expressions Compound Compound Expressions

Arithmetic Expressions can either be integer expressions or real expressions. Sometimes a mixed expressions can also be formed which is a mixer of real and integer expressions. Arithmetic Expressions can either be integer expressions or real expressions. Sometimes a mixed expressions can also be formed which is a mixer of real and integer expressions.

Integer Expressions are formed by connecting integer constants and/or integer variables using integer arithmetic operators. The following are valid integer expressions : int I,J,K,X,Y,Z,count; A) k - x B) k + x – y + count C) –j + k * y D) z % y

Real Expressions are formed by connecting real constants and/or real variables using real arithmetic operators. The following are valid real expressions: Real Expressions are formed by connecting real constants and/or real variables using real arithmetic operators. The following are valid real expressions: float qty,amount,,value; float qty,amount,,value; double fin,inter; const bal=250.53; double fin,inter; const bal=250.53; i) qty/amount i) qty/amount ii) (amount + qty*value)-bal ii) (amount + qty*value)-bal iii) fin + qty* inter iii) fin + qty* inter iv) inter – (qty * value) + fin iv) inter – (qty * value) + fin

The process of converting one predefined type into another is called Type Conversion. The process of converting one predefined type into another is called Type Conversion. C++ facilitates the type conversion in two forms: C++ facilitates the type conversion in two forms:

An implicit type conversion is a conversion performed by the compiler without programmer’s intervention. An implicit conversion is applied generally whenever differing data type are intermixed in an expression, so as not to lose information. An implicit type conversion is a conversion performed by the compiler without programmer’s intervention. An implicit conversion is applied generally whenever differing data type are intermixed in an expression, so as not to lose information.

An explicit type conversion is user-defined that forces an expression to be of specific type. An explicit type conversion is user-defined that forces an expression to be of specific type.

The C++ compiler converts all operands upto the type of the largest operand, which is called Type Promotion. The C++ compiler converts all operands upto the type of the largest operand, which is called Type Promotion.

The explicit conversion of an operand to a specific type is called type casting. The explicit conversion of an operand to a specific type is called type casting.

The expressions that result into 0(false) or 1(true) are called logical expressions. The logical expressions are combination of constants, variables and relational operators. The expressions that result into 0(false) or 1(true) are called logical expressions. The logical expressions are combination of constants, variables and relational operators.

A Logical Expressions may contain just one signed or unsigned variable or a constants, or it may have two or more variables or/and constants, or two or more expressions joined by valid relational and/or logical operators. Two or more variables or operators should not occur in continuation. A Logical Expressions may contain just one signed or unsigned variable or a constants, or it may have two or more variables or/and constants, or two or more expressions joined by valid relational and/or logical operators. Two or more variables or operators should not occur in continuation.

An expression is composed of one or more operations. An expression terminated by semicolon(;) becomes a statement. Statements form the smallest executable unit within a C++ program. An expression is composed of one or more operations. An expression terminated by semicolon(;) becomes a statement. Statements form the smallest executable unit within a C++ program. An assignment statement assigns value to a variable. The value assigned may be a constant, variable or a expression. An assignment statement assigns value to a variable. The value assigned may be a constant, variable or a expression. Example: a=cve; Example: a=cve;

The value of the right side (expression side) of the assignment is converted to the type of the left side (target variable). The value of the right side (expression side) of the assignment is converted to the type of the left side (target variable). => int x; char ch; ch=x;

C++ offers special shorthands that simplify the coding of a certain type of assignment statement. C++ offers special shorthands that simplify the coding of a certain type of assignment statement. Following are some example of C++ shorthands: Following are some example of C++ shorthands: x - = 10 ; equivalent to x = x – 10; x - = 10 ; equivalent to x = x – 10; x * = 3 ; equivalent to x = x * 3; x * = 3 ; equivalent to x = x * 3; x / = 2 ; equivalent to x = x/2 ; x / = 2 ; equivalent to x = x/2 ; x % = z ; equivalent to x = x % z ; x % = z ; equivalent to x = x % z ;

=>END