Upcoming SlideShare
×

# Operators and Expressions

1,391 views

Published on

Performing Simple Calculations with C++

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Operators and Expressions

1. 1. Performing Simple Calculations with C++ By Viel Joy Piston III-Saturn
2. 2. OPERATORS Arithmetic, Logical, Comparison, Assignment, Etc.
3. 3. OPERATOR An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
4. 4. C++ is rich in built-in operators and provides the following types of operators: •Unary Operators •Arithmetic Operators •Relational Operators •Logical Operators •Assignment Operators
5. 5. UNARY OPERATOR Unary Operator Requires single operand item to perform operation Assume variable A holds 10 then: Operator Description Example ++ Adds two operands A++ or ++A will give 11 -- Subtracts second operand from the first A-- or --A will give 9
6. 6. There are following arithmetic operators supported by C++ language: Assume variable A holds 10 and variable B holds 20 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 / Divide numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer division A * B will give 200 B % A will give 0
7. 7. RELATIONAL OPERATORS
8. 8. There are following relational operators supported by C++ language. Assume variable A holds 10 and variable B holds 20 then:
9. 9. Operator Description Example == Equal (A == B) is not true. != Not equal (A != B) is true. > Greater than (A > B) is not true. < Less than (A < B) is true. >= Greater than or equal to (A >= B) is not true. <= Less than or equal to (A <= B) is true.
10. 10. LOGICAL OPERATORS
11. 11. There are following logical operators supported by C++ language. Assume variable A holds 1 and variable B holds 0 then: Operator Description Example && Called Logical AND operator. If both the operands (A && B) is are non zero then condition becomes true. false. || 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.
12. 12. ASSIGNMENT OPERATORS
13. 13. There are following assignment operators supported by C++ language:
14. 14. Oper ator Description Example = 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 C += A is equivalent to result to left operand C=C+A -= Subtract AND assignment operator, It subtracts right operand from the left operand and C -= A is equivalent to assign the result to left operand C=C-A *= Multiply AND assignment operator, It multiplies right operand with the left operand and C *= A is equivalent to assign the result to left operand C=C*A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand %= Modulus AND assignment operator, It takes modulus using two operands and assign the C %= A is equivalent to result to left operand C=C%A <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C &2 ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^2 |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C |2 C /= A is equivalent to C=C/A
15. 15. Example 1. evaluate x=++y + 2y if y = 6? Solution :- Initially y = 6 =7 + 2* 7 =7 + 14 = 21 X = 21 Ans. Example 2. predict the output of following code segment ? Int n =7; Cout<<”++n =”<<++n << “n = “<<n<<”n”; : Solution = the output of the above code segment will be as shown below ++n = 8, n = 8
16. 16. EXPRESSIONS
17. 17. * Expression in C++ is form when we combine operands (variables and constant) and C++ Operators. * Expression can also be defined as: - a combination of Operands and Operators. •Operands •- are those values on which we want to perform operation.
18. 18. TYPES OF EXPRESSION
19. 19. ARITHMETIC EXPRESSION - expression in which arithmetic operators are used EXAMPLE: a + b=5 x-z=0
20. 20. RELATIONAL EXPRESSION - An expression in which relational operators are used Points about relational operators: 1.Relational operators are use to compare values. 2.All the expressions evaluates from left to right. 3.There are six relational operators in C++ programming (>,<,>=,<=,==,!=). 4.These operators evaluates results true or false. 5.False statement represent by 0 and True statement represent by 1.
21. 21. LOGICAL EXPRESSIONS
22. 22. EXAMPLE: Assume variable A holds 1 and variable B holds 0 then: (A&&B) is false (A||B) is true !(A||B) is true
23. 23. 1. There are three logical operators And( && ),or( || ) these two both are binary operator and not( ! ) is unary 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 one relation expression are use.
24. 24. Example 1 Write the corresponding C++ expression for the following mathematical expression. 1. 2 – ye2y + 4y 2. P + q /(r + s)4 Solution = 1. 2-y*exp(2*y)+4*y = 2. p+q/ pow ((r+s),4)