C Sharp Jn (2)
-
Upload
jahanullah -
Category
Technology
-
view
619 -
download
2
description
Transcript of C Sharp Jn (2)
Software Development Training Program
KaZIM HUSSAIn
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
Unary Operators Unary Operators are that operators which
require one operand to perform calculation.
-4; 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.
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--;
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.
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
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.
~
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.
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
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
Boolean Complement Operator
The ! Operator inverts the value of a boolean expression. So
!true results into false!false results into true
!
Binary Operators Binary Operators are that operators which
require two operand to perform calculation.
4/2;5>=7;
* / % + -
Arithmetic Operators
Binary OperatorsComparison Operator
Bitwise Operators
& ^ |
< <= > >=
== != is as
Short Circuit Logical Operators
&& ||
Assignment Operators
= *= /= %= +=
-= &= ^= |=
Binary Operators
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.
* / % + -
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
Comparison Operator
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
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.
& ^ |
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
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
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
Bitwise Operator
The &,^ and | behave in the same way when applied to boolean.
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
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
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
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.
&& ||
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.
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.
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.
Assignment Operators
= *= /= %= +=
-= &= ^=
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
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
Example
int a = 5;
int b = a == 5 ? 6 : 1;result b = 6;bool c = a<5 ? false : true;result c = ? //what
Operator Precedence
Precedence Associativity
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 = *= /= %= += -= <<= >>= &= ^= |=
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;
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;
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;
Escape Sequences
\’ – Single quote \” – Double quote \\ – Backslash \b – Backspace \n – New Line \r – Carriage Return \t – Tab
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
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
Exercise
Average Min, Max Separate the digits