Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.

Similar presentations


Presentation on theme: "LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations."— Presentation transcript:

1 LESSON 3 Operators

2 Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.

3 Expression  A sequence of one or more operands, and zero or more operators, that when combined, produce a value. C= A+B Operand Operator

4 Assignment Operator  Used for assignment a value to a variable and for performing computations. length = 25; cMyCar = “Mercedes”; sum = 3 + 7; newTotal = 18.3 * amount; slope = (y2-y1)/(x2-x1); a=b=c=25;

5 Arithmetic Operator OperatorMeaningExpression +AdditionA + B -SubtractionA - B *MultiplicationA * B /DivisionA / B %ModuloA % B

6 Precedence of Arithmetic Operators LevelOperatorAssociativity 1* / %Left to Right 2+ -Left to Right

7 Precedence of Arithmetic Operators 1. x = 2 + 3 * 4 2. x = 5 – 12 / 3 3. x = 5 + 7 % 2 4. x = 1 + 2 * 3 / 4 5. x = (2 + 3) * 4

8 Operating with Operators 1. #include 2. #include 3. using namespace std; 4. int main() 5. { int x,y; 6. cout<<“Enter first number”; 7. cin>> x; 8. cout<<”Enter second number”; 9. cin>> y; 10. cout<<”\n Sum is ”<<x+y; 11. cout<<”\n Difference is ”<<x-y; 12. cout<<”\n Product is ”<<x*y; 13. cout<<”\n Division is ”<<x/y; x Enter first number 14. getch(); 15. return 0; 16. } y Enter first number 9 Enter second number Enter first number 9 Enter second number 4 Enter first number 9 Enter second number 4 Sum is 13 Enter first number 9 Enter second number 4 Sum is 13 Difference is 5 Enter first number 9 Enter second number 4 Sum is 13 Difference is 5 Product is 36 Enter first number 9 Enter second number 4 Sum is 13 Difference is 5 Product is 36 Division is 2 9 x 4 y

9 Compound Assignment Operators value += increase; ExpressionIs equivalent to value= value + increase; a -= 5; a= a - 5; a /= b;a= a / b; price *= units + 1;price= price*(units+1);

10 Compound Assignment Operators x=3 1. x+=4*2 2. x*=2+2*3 3. x %= 5 + 7 % 2

11 Increment and Decrement B=3; A=++B; //A constrains 4, B contains 4 B=3; A=++B; //A constrains 4, B contains 4 B=3; A=B++; //A constrains 3, B contains 4 B=3; A=B++; //A constrains 3, B contains 4 prefix ++C;C = C+1; postfix C--;C = C-1;

12 Increment and Decrement x=3 1. a=x++ + 2 2. a=++x + 2 3. a= ++x - 1 + x++ 4. a= --x * 2 + x++

13 Relational Operators OperatorMeaning ==Equals to >Greater than <Less than >=Greater than or equal to <=Less than or equal to !=Not equal to

14 Relational Operators ExpressionResult a == 5false a*b >= 6true b+4 > a*cfalse (b=2) == atrue Suppose a=2, b=3, and c=6

15 Logical Operators OperatorMeaning !Not ||Or &&And

16 Not Operator a!a truefalse true

17 And Operator aba && b true false truefalse

18 Or Operator aba || b true falsetrue falsetrue false

19 Relational Operators ExpressionResult ! (5 == 5)false ((5==5)||(3>6))true ((5==5)&&(3>6))false

20 Precedence of Operators

21 (6*3==36/2) || (13<3*3+4) && !(6-2<5)

22 Operating with Operators 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int x=5, y=6; 7. bool z; 8. z = (x 6) && (x!=y); 9. cout<<z; 10. getch(); 11. return 0; 12. } 1

23 Conditional Operator condition ?result1:result2; condition result1 true result2 false

24 Conditional Operator condition ?result1:result2; 7 == 5 ?4:3; int a,b,c; a=2; b=7; c=(a>b) ? a : b; cout << c; 7

25 Operating with Operators 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int a=5, b=10, c=15; 7. a=b*4+c%5; 8. b=a+10; 9. c+=a; 10. cout<<a <<“ ” <<b <<“ ” <<c; 11. getch(); 12. return 0; 13. } 40 50 55

26 Operating with Operators 1. #include 2. #include 3. using namespace std; 4. int main( ) 5. { 6. int i=10; 7. int j=5; 8. int k=1; 9. k += i++ - ++j; 10. cout<<k <<endl; 11. cout<<i <<endl; 12. cout<<j <<endl; 5 11 6 13. getch(); 14. return 0; 15. }

27 1. i=10; j=5 k = i++ - ++j cout<<k<<i<<j; 2. i=3; j=4; k=5; result=i++ - j++ + --k; cout <<k<<i<<j cout<<result;


Download ppt "LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations."

Similar presentations


Ads by Google