lesson 3 operators. operators symbol that tells the computer to perform certain mathematical or...
Post on 18-Jan-2018
234 Views
Preview:
DESCRIPTION
TRANSCRIPT
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
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;
Arithmetic Operator
Operator
Meaning Expression
+ Addition A + B- Subtraction A - B* Multiplicatio
nA * B
/ Division A / B% Modulo A % B
Precedence of Arithmetic Operators
Level
Operator
Associativity
1 * / % Left to Right
2 + - Left to Right
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
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
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);
Compound Assignment Operators
x=31. x+=4*22. x*=2+2*33. x %= 5 + 7 % 2
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;
Increment and Decrement
x=31. a=x++ + 22. a=++x + 23. a= ++x - 1 + x++4. a= --x * 2 + x++
Relational OperatorsOperator Meaning
== Equals to
> Greater than< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
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
Logical OperatorsOperator Meaning
! Not
|| Or&& And
Not Operator
a !atrue falsefalse true
And Operator
a b a && btrue true truetrue false falsefalse true falsefalse false false
Or Operator
a b a || btrue true truetrue false truefalse true truefalse false false
Relational Operators
Expression Result! (5 == 5) false
((5==5)||(3>6)) true((5==5)&&(3>6)) false
Precedence of Operators
Precedence of Operators(6*3==36/2) || (13<3*3+4) && !(6-2<5)
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
Conditional Operator
condition ? result1 : result2 ;
condition
result1
true
result2
false
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
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
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.}
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;
top related