Upcoming SlideShare
×

# Operators and Expression

1,327 views
1,285 views

Published on

Published in: Education
1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• thanks to all

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total views
1,327
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
100
1
Likes
0
Embeds 0
No embeds

No notes for slide

### Operators and Expression

1. 1. An operator is a symbol thattells the compiler to perform specific mathematical or logical manipulations.
2. 2. C++ is rich in built-inoperators and provides followingtype of operators: •Unary Operator •Arithmetic Operators •Relational Operators •Logical Operators •Bitwise Operators •Assignment Operators •Misc Operators
3. 3. Unary Operator Requires single operanditem to perform operationAssume variable A holds 10 then:Operator Description Example A++ or ++A will give++ Adds two operands 11 Subtracts second operand-- A-- or --A will give 9 from the first
4. 4. There are following arithmetic operatorssupported by C++ language:Assume variable A holds 10 and variable B holds20 then:Operator Description Example+ Adds two operands A + B will give 30 Subtracts second operand from the- A - B will give -10 first* Multiply both operands A * B will give 200/ Divide numerator by de-numerator B / A will give 2 Modulus Operator and remainder% B % A will give 0 of after an integer division
5. 5. There are following relational operatorssupported by C++ language.Assume variable A holds 10 and variable B holds20 then:
6. 6. Opera Description Exampletor Checks if the value of two operands is equal or not, if yes then (A == B) is== condition becomes true. not true. Checks if the value of two operands is equal or not, if values are not (A != B) is!= equal then condition becomes true. true. Checks if the value of left operand is greater than the value of right (A > B) is > operand, if yes then condition becomes true. not true. Checks if the value of left operand is less than the value of right (A < B) is < operand, if yes then condition becomes true. true. Checks if the value of left operand is greater than or equal to the (A >= B) is >= value of right operand, if yes then condition becomes true. not true. Checks if the value of left operand is less than or equal to the value (A <= B) is <= of right operand, if yes then condition becomes true. true.
7. 7. There are following logical operators supportedby C++ language. Assume variable A holds 1 and variable B holds 0 then: Operator Description Example Called Logical AND operator. If both the operands are non && (A && B) is false. zero then condition becomes true. Called Logical OR Operator. If any of the two operands is || (A || B) is true. non zero then condition becomes true. Called Logical NOT Operator. Use to reverses the logical ! state of its operand. If a condition is true then Logical NOT !(A && B) is true. operator will make false.
8. 8. The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60 and variable B holds 13 then:Operator Description Example Binary AND Operator copies a bit to the result if it exists in both (A & B) will give 12 which is& operands. 0000 1100 (A | B) will give 61 which is| Binary OR Operator copies a bit if it exists in either operand. 0011 1101 Binary XOR Operator copies the bit if it is set in one operand but not (A ^ B) will give 49 which is^ both. 0011 0001 Binary Ones Complement Operator is unary and has the effect of (~A ) will give -60 which is~ flipping bits. 1100 0011 Binary Left Shift Operator. The left operands value is moved left by the A << 2 will give 240 which is<< number of bits specified by the right operand. 1111 0000 Binary Right Shift Operator. The left operands value is moved right by the A >> 2 will give 15 which is>> number of bits specified by the right operand. 0000 1111
9. 9. There are following assignment operatorssupported by C++ language:
10. 10. Oper Description Exampleator Simple assignment operator, Assigns values from right side operands to left side C = A + B will assign= operand value of A + B into C Add AND assignment operator, It adds right operand to the left operand and assign the C += A is equivalent to+= result to left operand C=C+A Subtract AND assignment operator, It subtracts right operand from the left operand C -= A is equivalent to-= and assign the result to left operand C=C-A Multiply AND assignment operator, It multiplies right operand with the left operand C *= A is equivalent to*= and assign the result to left operand C=C*A Divide AND assignment operator, It divides left operand with the right operand and C /= A is equivalent to/= assign the result to left operand C=C/A Modulus AND assignment operator, It takes modulus using two operands and assign C %= A is equivalent%= the result to left operand to C = C % A C <<= 2 is same as C =<<= Left shift AND assignment operator C << 2 C >>= 2 is same as C =>>= Right shift AND assignment operator C >> 2 C &= 2 is same as C =&= Bitwise AND assignment operator C&2 C ^= 2 is same as C =^= bitwise exclusive OR and assignment operator C^2 C |= 2 is same as C = C|= bitwise inclusive OR and assignment operator |2
11. 11. There are few other operators supported by C++ Language.Operator Description Size of operator returns the size of a variable. For example size of(a), where a isSize of integer, will return 4. Conditional operator. If Condition is true ? then it returns value X : otherwiseCondition ? X : Y value Y Comma 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 -> Member operators are used to reference individual members of classes,(arrow) structures, and unions. Casting operators convert one data type to another. For example, int(2.2000)Cast would return 2. Pointer operator & returns the address of an variable. For example &a; will give& actual address of the variable. Pointer operator * is pointer to a variable. For example *var; will pointer to a* variable var.
12. 12. *Expression in C++ is form when we combineoperands (variables and constant) and C++Operators.“*Expression can also be defined as:"Expression in C++ is a combination ofOperands and Operators.“*Operands in C++ Program are those valueson which we want to perform performoperation.
13. 13. "An expression in which arithmeticoperators are used is calledarithmetic expression“.For example an arithmetic expressionis look just like that a+b=5
14. 14. "A relational operator with constants andvariables makes relational expression or Anexpressions in which relational operators are useis called relational expression.Points about relational operators :-1.Relational operators are use to compare values.2.All the expressions evaluates from left toright.3.There are six relational operators in C++programming (>,<,>=,<=,==,!=).4.These operators evaluates results true orfalse.5.False statement represent by 0 and Truestatement represent by 1.6.These operators evaluate at statement level andhas no preference.
15. 15. 1. There are three logical operators And( && ),or( || ) these twoboth are binary operator and not( ! ) is u nary operator.2. More than one relation expression are combine by using logical operators.3. The expression will evaluate from left to right if more than onerelation expression are use.
16. 16. Example 1 . write the corresponding C++expression for the following mathematicalexpression ?1.2 – ye2y + 4y2.P + q /(r + s)4Solution = 1. 2-y*exp(2*y)+4*y = 2. p+q/ pow ((r+s),4)