Upcoming SlideShare
×

# Operators

1,074 views

Published on

operators

1 Like
Statistics
Notes
• Full Name
Comment goes here.

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

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

No notes for slide

### Operators

1. 1. Software Development Training Program Mr. Zia Khan, and Mr. Zeeshan Hanif
3. 3. Operators <ul><li>Operators can be used to combine or alter the program values. There are three types of operators: - </li></ul><ul><li>1. Unary Operators </li></ul><ul><li>2. Binary Operators </li></ul><ul><li>3. Ternary Operators </li></ul>
4. 4. Unary Operators <ul><li>Unary Operators are that operators which require one operand to perform calculation. </li></ul><ul><li>-4; </li></ul><ul><li>5++; </li></ul>! - + ~ -- ++
5. 5. Increment and Decrement Operator The ++ and – are increment and decrement operators. The increment operator increases its operand by one; the decrement operator decreases its operand by one. -- ++
6. 6. Increment and Decrement Operator <ul><li>The expression </li></ul><ul><li>a = a + 1; </li></ul><ul><li>can written using increment operator as: </li></ul><ul><li>a++; </li></ul><ul><li>Similarly the statement </li></ul><ul><li>a = a – 1; </li></ul><ul><li>Is equal to following: </li></ul><ul><li>a--; </li></ul>
7. 7. Increment and Decrement Operator <ul><li>The operator a++ first assign the value and then increment a by one. </li></ul><ul><li>These operators can be used as: </li></ul><ul><li>++a; </li></ul><ul><li>--a; </li></ul><ul><li>In this case the increment and decrement is done before assignment. </li></ul>
8. 8. Examples 6 5 b = a++ 5 4 4 b = --a 5 4 5 b = a-- 5 6 6 b = ++a 5 Final value of a Final value of b Expression Initial value of a
9. 9. Bitwise Inversion Operator <ul><li>This operator performs bitwise inversion on integral types. This operator works by converting all the 1 bits in a binary to 0s and all the 0 to 1s. </li></ul>~
10. 10. Bitwise Inversion Operator <ul><li>For example binary representation : </li></ul><ul><li>01001101 </li></ul><ul><li>Using the ~ operator convert into following </li></ul><ul><li>10110010 </li></ul><ul><li>You may notice that all the 0 bits are converted into 1s and all the 1 bits are converted into 0s. </li></ul>
11. 11. Bitwise Inversion Operator <ul><li>For a positive value the result is always negative and increase the value by one. </li></ul><ul><li>For example: </li></ul><ul><li>~15 returns -16 </li></ul><ul><li>~1128 returns -1129 </li></ul><ul><li>~0 returns -1 </li></ul><ul><li>~8888888 returns -8888889 </li></ul>
12. 12. Bitwise Inversion Operator <ul><li>For a negative value the result is always positive and decrease the value by one. </li></ul><ul><li>For example: </li></ul><ul><li>~-15 returns 14 </li></ul><ul><li>~-1128 returns 1127 </li></ul><ul><li>~-1 returns 0 </li></ul><ul><li>~-88888 returns 88887 </li></ul>
13. 13. Boolean Complement Operator <ul><li>The ! Operator inverts the value of a boolean expression. So </li></ul><ul><li>!true results into false </li></ul><ul><li>!false results into true </li></ul>!
14. 14. Binary Operators <ul><li>Binary Operators are that operators which require two operand to perform calculation. </li></ul><ul><li>4 / 2; </li></ul><ul><li>5 >= 7; </li></ul>Arithmetic Operators - + % / *
15. 15. Binary Operators Comparison Operator Bitwise Operators Short Circuit Logical Operators | ^ & as is != == >= > <= < || &&
16. 16. Binary Operators Assignment Operators ^= & = -= += % = /= *= =
17. 17. Arithmetic Operators <ul><li>Basic arithmetic operators are addition(+), subtraction(-), multiplication(*), and division(/). All behave the same, as you would expect for all numeric types. Modulus(%) operator returns the remainder of a division operation. </li></ul>- + % / *
18. 18. Comparison Operator <ul><li>These are also called relational operators. They determine the relationship that one operand has to the other. The outcome of these operators is always a boolean value. </li></ul>as is != == >= > <= <
19. 19. Comparison Operator Less than or equal to <= Greater than or equal to >= Less than < Greater than > Not equal to != Equal to == Result Operator
20. 20. Bitwise Operator <ul><li>The bitwise operators provides bitwise AND, XOR and OR operations respectively. These operators can be apply to both integer types and boolean types. These operators compare each bit of first operand with the corresponding bit of the second operand and give results according to following rules. </li></ul>| ^ &
21. 21. Bitwise Operator (&) <ul><li>For AND operation 1 AND 1 results 1. any other combination produces 0. </li></ul>1 1 0 0 Op1 1 1 0 0 0 1 0 0 Op1 AND Op2 Op2
22. 22. Bitwise Operator (|) <ul><li>For OR operations, 0 OR 0 produces 0. any other combination produces 1. </li></ul>1 1 0 0 Op1 1 1 1 0 1 1 0 0 Op1 OR Op2 Op2
23. 23. Bitwise Operator (^) <ul><li>For XOR operations, 1 XOR 0 produces 1, as 0 XOR 1 does, any other combination produces 0. </li></ul>1 1 0 0 Op1 0 1 1 0 1 1 0 0 Op1 XOR Op2 Op2
24. 24. Bitwise Operator <ul><li>The &,^ and | behave in the same way when applied to boolean. </li></ul>
25. 25. Bitwise Operator (&) <ul><li>For AND operation true AND true results true. any other combination produces false. </li></ul>true true false false Op1 true true false false false true false false Op1 AND Op2 Op2
26. 26. Bitwise Operator (|) <ul><li>For OR operations, false OR false produces false. any other combination produces true. </li></ul>true true false false Op1 true true true false true true false false Op1 OR Op2 Op2
27. 27. Bitwise Operator (^) <ul><li>For XOR operations, true XOR false produces true, as false XOR true does, any other combination produces false. </li></ul>true true false false Op1 false true true false true true false false Op1 XOR Op2 Op2
28. 28. Short Circuit Logical Operators <ul><li>The short circuit logical operators && and || provide logical AND and OR operations on boolean types similar to the & and | operators. </li></ul><ul><li>However they have a valuable additional feature. </li></ul>|| &&
29. 29. Short Circuit Logical Operators <ul><li>For an AND operation, if first operand is false, the result is false without checking the other operand. </li></ul><ul><li>For an OR operation, if first operand is true, the result is true, without checking the other operand. </li></ul>
30. 30. Example <ul><li>int a = 5; </li></ul><ul><li>bool b = ( (a>8) && (a==5) ); </li></ul><ul><li>The first expression (a>8) returns false so the second expression (a==5) never executes and false is stored in b. </li></ul>
31. 31. Example <ul><li>int a = 5; </li></ul><ul><li>bool b = ( (a>3) | | (a==2) ); </li></ul><ul><li>The first expression (a>3) returns true so the second expression (a==5) never executes and true is stored in b. </li></ul>
32. 32. Assignment Operators ^= & = -= += % = /= *= =
33. 33. Example <ul><li>int a = 6; </li></ul><ul><li>int b = 7; </li></ul><ul><li>a += b; result a=13 </li></ul><ul><li>a *= b; result a=42 </li></ul><ul><li>a /= b; result a=1 </li></ul><ul><li>a &= b; result a=6 </li></ul><ul><li>a ^= b; result a=1 </li></ul>
34. 34. Ternary Operators <ul><li>Ternary Operators are that operators which require three operands to perform calculation. There is only one ternary operator in C#. </li></ul><ul><li>?: </li></ul><ul><li>(5>3) ? Value1 : Value2 </li></ul><ul><li>If (5>3) results true then Value1 is assign </li></ul><ul><li>otherwise Value2 is assign </li></ul>
35. 35. Example <ul><li>int a = 5; </li></ul><ul><li>int b = a == 5 ? 6 : 1; </li></ul><ul><li>result b = 6; </li></ul><ul><li>bool c = a<5 ? false : true; </li></ul><ul><li>result c = ? //what </li></ul>
36. 36. Operator Precedence <ul><li>Precedence </li></ul>
37. 37. Operator Precedence = *= /= %= += -= <<= >>= &= ^= |= Assignment ?: Conditional || Conditional OR && Conditional AND | Logical OR ^ Logical XOR & Logical And == != Equality < > <= >= is as Relational << >> Shift + - Additive * / % Multiplicative = - ! ~ ++x --x (T)x Unary (x) x.y f(x) a[x] x++ x-- new typeof sizeof checked unchecked Primary Operators Category
38. 38. Examples <ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a & b + a | b; </li></ul><ul><li>result c = 13; </li></ul><ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a | b + a & b; </li></ul><ul><li>result c = 13; </li></ul>
39. 39. Examples <ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a & b + ( a | b ) ; </li></ul><ul><li>result c = 9; </li></ul><ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a + b * 2 – a / 4 ; </li></ul><ul><li>result c = 31; </li></ul>
40. 40. Examples <ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a + b * (2 - a) / 4; </li></ul><ul><li>result c = -12; </li></ul><ul><li>int a = 9; </li></ul><ul><li>int b = 12; </li></ul><ul><li>int c = a + b * ( (2 - a) / 4 ) ; </li></ul><ul><li>result c = -3; </li></ul>
41. 41. Escape Sequences <ul><li>’ – Single quote </li></ul><ul><li>” – Double quote </li></ul><ul><li>– Backslash </li></ul><ul><li> – Backspace </li></ul><ul><li> – New Line </li></ul><ul><li> – Carriage Return </li></ul><ul><li> – Tab </li></ul>
42. 42. Math class Math.Round(4.9) is 5 Math.Round(4.2) is 4 Math.Round(4.5) is 4 Rounds a value to the nearest integer Round(x) Math.Min(5,9) is 5 Math.Min(5,-9) is -9 Returns the smaller of two number Min(x,y) Math.Max(5,9) is 9 Math.Max(5,-9) is 5 Returns the larger of two number Max(x,y) Math.Floor(5.4) is 5 Math.Floor(5.9) is 5 Returns the largest integer less then or equal to x Floor(x) Math.Ceiling(5.4) is 6 Math.Ceiling(5.9) is 6 Returns the smallest integer greater than or equal to the x Ceiling(x) Math.Abs(-45) is 45 Math.Abs(45) is 45 Returns the absolute value of x Abs(x) Example Description Methods
43. 43. Math class Math.Truncate(38.4) is 38 Math.Truncate(38.485487) is 38 Returns the integral part of x Truncate(x) Math.Sqrt(9) is 3 Math.Sqrt(5) is 2.23606797749979 Returns the square root of x Sqrt(x) Math.Pow(4.2) is 16 Returns x raised to the power y Pow(x,y) Example Description Methods
44. 44. Exercise <ul><li>Average </li></ul><ul><li>Min, Max </li></ul><ul><li>Separate the digits </li></ul>