Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

3 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.

4 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.

5 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 11 -- Decrement operatorDecrement operator, decreases integer value by one A-- will give 9

6 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

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

8 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.

9 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

10 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

11 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.

12 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)

13 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.

14 Truth Table ab Value of the expression a && ba || b 0000 0101 1001 111 1

15 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

16 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

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

18 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.

19 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 : + -

20 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

21 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

22 Example 1 Evaluate x1=(-b+ sqrt (b*b-4*a*c))/(2*a) @ 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) =(5 + 1.0)/(2*1) =(6.0)/(2*1) =6.0/2 = 3.0

23

24

25 Arithmetic Arithmetic Expressions Relational Relational Expressions Compound Compound Expressions

26 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.

27 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

28 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

29 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:

30 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.

31 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.

32 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.

33 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.

34 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.

35 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.

36 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;

37 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;

38

39 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 ;

40 =>END


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

Similar presentations


Ads by Google