lesson 3 operators. operators symbol that tells the computer to perform certain mathematical or...

27
LESSON 3 Operators

Upload: mabel-goodwin

Post on 18-Jan-2018

234 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

LESSON 3

Operators

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

Operators Symbol that tells the computer to

perform certain mathematical or logical manipulations.

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

Expression A sequence of one or more

operands, and zero or more operators, that when combined, produce a value.

C = A + BOperand

Operator

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

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;

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

Arithmetic Operator

Operator

Meaning Expression

+ Addition A + B- Subtraction A - B* Multiplicatio

nA * B

/ Division A / B% Modulo A % B

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

Precedence of Arithmetic Operators

Level

Operator

Associativity

1 * / % Left to Right

2 + - Left to Right

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

Precedence of Arithmetic Operators

1. x = 2 + 3 * 42. x = 5 – 12 / 33. x = 5 + 7 % 24. x = 1 + 2 * 3 / 45. x = (2 + 3) * 4

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

Operating with Operators1. #include <iostream>2. #include <conio.h>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 9Enter first number 9Enter second number

Enter first number 9Enter second number 4

Enter first number 9Enter second number 4Sum is 13

Enter first number 9Enter second number 4Sum is 13Difference is 5

Enter first number 9Enter second number 4Sum is 13Difference is 5Product is 36

Enter first number 9Enter second number 4Sum is 13Difference is 5Product is 36Division is 2

9x

4y

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

Compound Assignment Operators

value += increase;

Expression Is equivalent to

value= value + increase;

a -= 5; a= a - 5;

a /= b; a= a / b;

price *= units + 1; price= price*(units+1);

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

Compound Assignment Operators

x=31. x+=4*22. x*=2+2*33. x %= 5 + 7 % 2

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

Increment and Decrement

B=3;A=++B;//A constrains 4, B contains 4

B=3;A=B++;//A constrains 3, B contains 4

prefix ++C; C = C+1;

postfix C--; C = C-1;

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

Increment and Decrement

x=31. a=x++ + 22. a=++x + 23. a= ++x - 1 + x++4. a= --x * 2 + x++

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

Relational OperatorsOperator Meaning

== Equals to

> Greater than< Less than

>= Greater than or equal to

<= Less than or equal to

!= Not equal to

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

Relational Operators

Expression Resulta == 5 false

a*b >= 6 trueb+4 > a*c false(b=2) == a true

Suppose a=2, b=3, and c=6

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

Logical OperatorsOperator Meaning

! Not

|| Or&& And

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

Not Operator

a !atrue falsefalse true

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

And Operator

a b a && btrue true truetrue false falsefalse true falsefalse false false

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

Or Operator

a b a || btrue true truetrue false truefalse true truefalse false false

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

Relational Operators

Expression Result! (5 == 5) false

((5==5)||(3>6)) true((5==5)&&(3>6)) false

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

Precedence of Operators

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

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

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

Operating with Operators1. #include <iostream>2. #include <conio.h>3. using namespace std;4. int main( )5. {6. int x=5, y=6;7. bool z; 8. z = (x<=5) || !(y>6) && (x!

=y);9. cout<<z;10. getch();11. return 0;12.}

1

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

Conditional Operator

condition ? result1 : result2 ;

condition

result1

true

result2

false

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

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

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

Operating with Operators1. #include <iostream>2. #include <conio.h>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

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

Operating with Operators1. #include <iostream>2. #include <conio.h>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.}

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

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<<jcout<<result;