c sharp jn (2)

43
Software Development Training Program KaZIM HUSSAIn

Upload: jahanullah

Post on 05-Dec-2014

619 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C Sharp Jn (2)

Software Development Training Program

KaZIM HUSSAIn

Page 2: C Sharp Jn (2)

Operators

Operators can be used to combine or alter the program values. There are three types of operators: -

1. Unary Operators 2. Binary Operators 3. Ternary Operators

Page 3: C Sharp Jn (2)

Unary Operators Unary Operators are that operators which

require one operand to perform calculation.

-4; 5++;

++ -- + ! - ~

Page 4: C Sharp Jn (2)

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.

Page 5: C Sharp Jn (2)

Increment and Decrement Operator

The expression a = a + 1;

can written using increment operator as:a++;

Similarly the statement a = a – 1;

Is equal to following:a--;

Page 6: C Sharp Jn (2)

Increment and Decrement Operator

The operator a++ first assign the value and then increment a by one.

These operators can be used as: ++a; --a; In this case the increment and

decrement is done before assignment.

Page 7: C Sharp Jn (2)

Initial value of

a

Expression

Final value of

b

Final value of

a

5b = a+

+5 6

5b = +

+a6 6

5 b = a-- 5 4

5 b = --a 4 4

Examples

Page 8: C Sharp Jn (2)

Bitwise Inversion Operator

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.

~

Page 9: C Sharp Jn (2)

Bitwise Inversion Operator

For example binary representation :01001101

Using the ~ operator convert into following

10110010You may notice that all the 0 bits are

converted into 1s and all the 1 bits are converted into 0s.

Page 10: C Sharp Jn (2)

Bitwise Inversion Operator

For a positive value the result is always negative and increase the value by one.

For example:

~15 returns -16~1128 returns -1129~0 returns -1~8888888 returns -8888889

Page 11: C Sharp Jn (2)

Bitwise Inversion Operator

For a negative value the result is always positive and decrease the value by one.

For example:

~-15 returns 14~-1128 returns 1127~-1 returns 0~-88888 returns 88887

Page 12: C Sharp Jn (2)

Boolean Complement Operator

The ! Operator inverts the value of a boolean expression. So

!true results into false!false results into true

!

Page 13: C Sharp Jn (2)

Binary Operators Binary Operators are that operators which

require two operand to perform calculation.

4/2;5>=7;

* / % + -

Arithmetic Operators

Page 14: C Sharp Jn (2)

Binary OperatorsComparison Operator

Bitwise Operators

& ^ |

< <= > >=

== != is as

Short Circuit Logical Operators

&& ||

Page 15: C Sharp Jn (2)

Assignment Operators

= *= /= %= +=

-= &= ^= |=

Binary Operators

Page 16: C Sharp Jn (2)

Arithmetic Operators

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.

* / % + -

Page 17: C Sharp Jn (2)

Comparison Operator

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.

< <= > >=

== != is as

Page 18: C Sharp Jn (2)

Comparison Operator

Operator Result

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

Page 19: C Sharp Jn (2)

Bitwise Operator

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.

& ^ |

Page 20: C Sharp Jn (2)

Bitwise Operator (&) For AND operation 1 AND 1 results 1.

any other combination produces 0.

Op1 Op2 Op1 AND Op2

0 0 0

0 1 0

1 0 0

1 1 1

Page 21: C Sharp Jn (2)

Bitwise Operator (|) For OR operations, 0 OR 0 produces

0. any other combination produces 1.

Op1 Op2 Op1 OR Op2

0 0 0

0 1 1

1 0 1

1 1 1

Page 22: C Sharp Jn (2)

Bitwise Operator (^) For XOR operations, 1 XOR 0

produces 1, as 0 XOR 1 does, any other combination produces 0.

Op1 Op2 Op1 XOR Op2

0 0 0

0 1 1

1 0 1

1 1 0

Page 23: C Sharp Jn (2)

Bitwise Operator

The &,^ and | behave in the same way when applied to boolean.

Page 24: C Sharp Jn (2)

Bitwise Operator (&) For AND operation true AND true

results true. any other combination produces false.

Op1 Op2 Op1 AND Op2

false false false

false true false

true false false

true true true

Page 25: C Sharp Jn (2)

Bitwise Operator (|) For OR operations, false OR false

produces false. any other combination produces true.

Op1 Op2 Op1 OR Op2

false false false

false true true

true false true

true true true

Page 26: C Sharp Jn (2)

Bitwise Operator (^) For XOR operations, true XOR false

produces true, as false XOR true does, any other combination produces false.

Op1 Op2 Op1 XOR Op2

false false false

false true true

true false true

true true false

Page 27: C Sharp Jn (2)

Short Circuit Logical Operators

The short circuit logical operators && and || provide logical AND and OR operations on boolean types similar to the & and | operators.

However they have a valuable additional feature.

&& ||

Page 28: C Sharp Jn (2)

Short Circuit Logical Operators

For an AND operation, if first operand is false, the result is false without checking the other operand.

For an OR operation, if first operand is true, the result is true, without checking the other operand.

Page 29: C Sharp Jn (2)

Example

int a = 5;bool b = ( (a>8) && (a==5) );

The first expression (a>8) returns false so the second expression (a==5) never executes and false is stored in b.

Page 30: C Sharp Jn (2)

Example

int a = 5;bool b = ( (a>3) | | (a==2) );

The first expression (a>3) returns true so the second expression (a==5) never executes and true is stored in b.

Page 31: C Sharp Jn (2)

Assignment Operators

= *= /= %= +=

-= &= ^=

Page 32: C Sharp Jn (2)

Example

int a = 6;int b = 7;a+=b; result a=13a*=b; result a=42a/=b; result a=1a&=b; result a=6a^=b; result a=1

Page 33: C Sharp Jn (2)

Ternary Operators

Ternary Operators are that operators which require three operands to perform calculation. There is only one ternary operator in C#.

?:(5>3) ? Value1 : Value2If (5>3) results true then Value1 is

assignotherwise Value2 is assign

Page 34: C Sharp Jn (2)

Example

int a = 5;

int b = a == 5 ? 6 : 1;result b = 6;bool c = a<5 ? false : true;result c = ? //what

Page 35: C Sharp Jn (2)

Operator Precedence

Precedence Associativity

Page 36: C Sharp Jn (2)

Operator PrecedenceCategory Operators

Primary (x) x.y f(x) a[x] x++ x-- new typeof sizeof checked unchecked

Unary = - ! ~ ++x --x (T)x

Multiplicative * / %

Additive + -

Shift << >>

Relational < > <= >= is as

Equality == !=

Logical And &

Logical XOR ^

Logical OR |

Conditional AND

&&

Conditional OR

||

Conditional ?:

Assignment = *= /= %= += -= <<= >>= &= ^= |=

Page 37: C Sharp Jn (2)

Examples

int a = 9;int b = 12;int c = a & b + a | b;result c = 13;

int a = 9;int b = 12;int c = a | b + a & b;result c = 13;

Page 38: C Sharp Jn (2)

Examples

int a = 9;int b = 12;int c = a & b + (a | b);result c = 9;

int a = 9;int b = 12;int c = a + b * 2 – a / 4 ;result c = 31;

Page 39: C Sharp Jn (2)

Examples

int a = 9;int b = 12;int c = a + b * (2 - a) / 4;result c = -12;

int a = 9;int b = 12;int c = a + b * ((2 - a) / 4);result c = -3;

Page 40: C Sharp Jn (2)

Escape Sequences

\’ – Single quote \” – Double quote \\ – Backslash \b – Backspace \n – New Line \r – Carriage Return \t – Tab

Page 41: C Sharp Jn (2)

Math classMethods Description Example

Abs(x) Returns the absolute value of x Math.Abs(-45) is 45Math.Abs(45) is 45

Round(x) Rounds a value to the nearest integer

Math.Round(4.9) is 5Math.Round(4.2) is 4Math.Round(4.5) is 4

Ceiling(x) Returns the smallest integer greater than or equal to the x

Math.Ceiling(5.4) is 6Math.Ceiling(5.9) is 6

Floor(x) Returns the largest integer less then or equal to x

Math.Floor(5.4) is 5Math.Floor(5.9) is 5

Max(x,y) Returns the larger of two number Math.Max(5,9) is 9Math.Max(5,-9) is 5

Min(x,y) Returns the smaller of two number Math.Min(5,9) is 5Math.Min(5,-9) is -9

Page 42: C Sharp Jn (2)

Math classMethods Description Example

Pow(x,y) Returns x raised to the power y Math.Pow(4,2) is 16

Sqrt(x) Returns the square root of x Math.Sqrt(9) is 3Math.Sqrt(5) is 2.23606797749979

Truncate(x)

Returns the integral part of x Math.Truncate(38.4) is 38Math.Truncate(38.485487) is 38

Page 43: C Sharp Jn (2)

Exercise

Average Min, Max Separate the digits