3/13/2018
What is an Operator?
Operator is an operation performed over data
at runtime
Operators and Expressions Takes one or more arguments (operands)
Performing Simple Calculations with C# Produces a new value
Operators have precedence
Precedence defines which will be evaluated first
Expressions are sequences of operators and
operands that are evaluated to a single value
2
Operators in C# Categories of Operators in C#
Operators in C# : Category Operators
Unary – take one operand Arithmetic + - * / % ++ --
Logical && || ^ !
Binary – take two operands
Binary & | ^ ~ << >>
Ternary (?:) – takes three operands Comparison == != < > <= >=
Except for the assignment operators, all = += -= *= /= %= &= |=
Assignment
binary operators are left-associative ^= <<= >>=
String concatenation +
The assignment operators and the
Type conversion is as typeof
conditional operator (?:) are right-associative Other . [] () ?: new
3 4
Operators Precedence Operators Precedence (2)
Precedence Operators Precedence Operators
Highest () Higher |
++ -- (postfix) new typeof &&
++ -- (prefix) + - (unary) ! ~ ||
* / % ?:
+ - = *= /= %= += -= <<= >>= &= ^=
Lowest
<< >> |=
< > <= >= is as Parenthesis operator always has highest
== != precedence
&
Note: prefer using parentheses, even when it
Lower ^
seems stupid to do so
5 6
1
3/13/2018
Arithmetic Operators Arithmetic Operators – Example
Arithmetic operators +, -, * are the same as in int squarePerimeter = 17;
double squareSide = squarePerimeter / 4.0;
math double squareArea = squareSide * squareSide;
Console.WriteLine(squareSide); // 4.25
Division operator /
if used on integers returns Console.WriteLine(squareArea); // 18.0625
integer (without rounding) or exception int a = 5;
int b = 4;
Division operator /
if used on real numbers Console.WriteLine( a + b ); // 9
returns real number or Infinity or NaN Console.WriteLine( a + b++ ); // 9
Console.WriteLine( a + b ); // 10
Remainder operator % returns the remainder Console.WriteLine( a + (++b) ); // 11
Console.WriteLine( a + b ); // 11
from division of integers
Console.WriteLine(12 / 3); // 4
The special addition operator ++ increments a Console.WriteLine(11 / 3); // 3
variable 7 8
Arithmetic Operators – Arithmetic Operators –
Example (2) Overflow Examples
Console.WriteLine(11.0 / 3); // 3.666666667 int bigNum = 2000000000;
Console.WriteLine(11 / 3.0); // 3.666666667 int bigSum = 2 * bigNum; // Integer overflow!
Console.WriteLine(11 % 3); // 2 Console.WriteLine(bigSum); // -294967296
Console.WriteLine(11 % -3); // 2
Console.WriteLine(-11 % 3); // -2 bigNum = Int32.MaxValue;
bigNum = bigNum + 1;
Console.WriteLine(1.5 / 0.0); // Infinity Console.WriteLine(bigNum); // -2147483648
Console.WriteLine(-1.5 / 0.0); // -Infinity
Console.WriteLine(0.0 / 0.0); // NaN checked
{
int x = 0; // This will cause OverflowException
Console.WriteLine(5 / x); // DivideByZeroException bigSum = bigNum * 2;
}
9 10
Logical Operators Logical Operators – Example
Logical operators take boolean operands and Using the logical operators:
return boolean result
bool a = true;
Operator ! turns true to false and false bool b = false;
to true Console.WriteLine(a && b); // False
Console.WriteLine(a || b); // True
Behavior of the operators &&, || and ^ Console.WriteLine(a ^ b); // True
(1 == true, 0 == false) : Console.WriteLine(!b); // True
Console.WriteLine(b || true); // True
Console.WriteLine(b && true); // False
Operation || || || || && && && && ^ ^ ^ ^
Console.WriteLine(a || true); // True
Operand1 0 0 1 1 0 0 1 1 0 0 1 1 Console.WriteLine(a && true); // True
Operand2 0 1 0 1 0 1 0 1 0 1 0 1 Console.WriteLine(!a); // False
Result 0 1 1 1 0 0 0 1 0 1 1 0 Console.WriteLine((5>7) ^ (a==b)); // False
11 12
2
3/13/2018
Comparison Operators Assignment Operators
Comparison operators are used to compare Assignment operators are used to assign a
variables value to a variable ,
==, <, >, >=, <=, != =, +=, -=, |=, ...
Comparison operators example: Assignment operators example:
int a = 5; int x = 6;
int b = 4; int y = 4;
Console.WriteLine(a >= b); // True Console.WriteLine(y *= 2); // 8
Console.WriteLine(a != b); // True int z = y = 3; // y=3 and z=3
Console.WriteLine(a == b); // False Console.WriteLine(z); // 3
Console.WriteLine(a == a); // True Console.WriteLine(x |= 1); // 7
Console.WriteLine(a != ++b); // False Console.WriteLine(x += 3); // 10
Console.WriteLine(a > b); // False Console.WriteLine(x /= 2); // 5
13 14
Other Operators Other Operators (2)
String concatenation operator + is used to Member access operator . is used to access
concatenate strings object members
If the second operand is not a string, it is Square brackets[] are used with arrays
converted to string automatically indexers and attributes
string first = "First"; Parentheses ( ) are used to override the
string second = "Second"; default operator precedence
Console.WriteLine(first + second);
// FirstSecond Class cast operator (type) is used to cast one
string output = "The number is : ";
int number = 5; compatible type to another
Console.WriteLine(output + number);
// The number is : 5
15 16
Other Operators (3) Other Operators – Example
Conditional operator ?: has the form Using some other operators:
b ? x : y int a = 6;
int b = 4;
(if b is true then the result is x else the result is y) Console.WriteLine(a > b ? "a>b" : "b>=a"); // a>b
Console.WriteLine((long) a); // 6
The new operator is used to create new objects
int c = b = 3; // b=3; followed by c=3;
The typeof operator returns System.Type Console.WriteLine(c); // 3
Console.WriteLine(a is int); // True
object (the reflection of a type) Console.WriteLine((a+b)/2); // 4
Console.WriteLine(typeof(int)); // System.Int32
The is operator checks if an object is
int d = new int();
compatible with given type
Console.WriteLine(d); // 0
17 18
3
3/13/2018
Implicit Type Conversion Explicit Type Conversion
Implicit type conversion Explicit type conversion
Automatic conversion of value of one data type Manual conversion of a value of one data type
to value of another data type to a value of another data type
Allowed when no loss of data is possible Allowed only explicitly by (type) operator
"Larger" types can implicitly take values of Required when there is a possibility of loss of
smaller "types" data or precision
Example: Example:
int i = 5; long l = 5;
long l = i; int i = (int) l;
19 20
Type Conversions – Example Expressions
Example of implicit and explicit conversions: Expressions are sequences of operators,
literals and variables that are evaluated to
float heightInMeters = 1.74f; // Explicit conversion
double maxHeight = heightInMeters; // Implicit
some value
Examples:
double minHeight = (double) heightInMeters; // Explicit
float actualHeight = (float) maxHeight; // Explicit int r = (150-20) / 2 + 5; // r=70
// Expression for calculation of circle area
float maxHeightFloat = maxHeight; // Compilation error!
double surface = Math.PI * r * r;
// Expression for calculation of circle perimeter
Note: Explicit conversion may be used even if double perimeter = 2 * Math.PI * r;
not required by the compiler
21 22
Expressions (2) Exercises
Expressions has: 1. Write an expression that checks if given integer is odd or
even.
Type (integer, real, boolean, ...) 2. Write a boolean expression that checks for given integer if it
Expression can be divided (without remainder) by 7 and 5 in the same
Value of type int. time.
Expression of type Calculated
Examples: int. Calculated at 3. Write an expression that calculates rectangle’s area by given
at runtime. width and height.
compile time.
4. Write an expression that checks for given integer if its third
int a = 2 + 3; // a = 5 digit (right-to-left) is 7. E. g. 1732 true.
int b = (a+3) * (a-4) + (2*a + 7) / 4; // b = 12 7. Write an expression that checks if given positive integer
bool greater = (a > b) || ((a == 0) && (b == 0));
number n (n ≤ 100) is prime. E.g. 37 is prime.
8. Write an expression that calculates trapezoid's area by given
Expression of type bool.
sides a and b and height h.
Calculated at runtime.
23 24