2. 8/30/20162
Operator
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
C# is rich in built-in operators and provides the following kinds of
operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
4. 8/30/20164
Logical Operators (Conditional
Operators)
Logical operators allows you to combine multiple conditions.
These operators involves at least two conditions and the final
Boolean result is determined by the operator being used.
AND
OR
XOR
NOT
Logical Operators
5. 8/30/20165
Operand and Operators
In all computer languages, expressions consist of two types of
components: operands and operators.
Operands are the objects that are manipulated.
Operators are the symbols that represent specific actions.
example:
5 + x
Note:
All expressions have at least one operand.
operator
operands
6. 8/30/20166
AND Operator (&&)
The AND operator is used to compare two Boolean values.
It returns true if both of the operands are true.
a b Result (AND)
0 0 0
0 1 0
1 0 0
1 1 1
0 – false
1 - true
7. 8/30/20167
AND Operator in Programming
name = "Steven";
password = "Steven123";
// evaluating both expression and returns true if all are true.
if (name == "Steven" && password == "Steven123")
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show "Unauthorised access");
}
8. 8/30/20168
OR Operator (||)
The OR operator is similar to AND, as it is used to compare two
Boolean values.
The OR operator returns true if either of the operand(s) are true.
a b Result (OR)
0 0 0
0 1 1
1 0 1
1 1 1
}
0 – false
1 - true
9. 8/30/20169
OR Operator in Programming
string username, password;
if ((username == "Steven" || username == “Clark")
&& (password == "Steven123"))
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show("Unauthorised access");
}
10. 8/30/201610
NOT Operator (!)
The NOT operator is a unary operator, as it operates on a single
operand.
The NOT operator inverts the value of a Boolean value.
If the original value is true then the returned value is false; if the
original value is false, the return value is true.
a Result (NOT)
0 1
1 0
0 – false
1 - true
11. 8/30/201611
NOT Operator in Programming
name = "Steven";
password = "Steven123";
// evaluating both expression and returns true if all are true.
if ( !(name == "Steven" && password == "Steven123"))
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show("Unauthorised access. Aborting…");
}
12. 8/30/201612
XOR Operator (^)
It returns false if the following condition matches:
(i) if both or all the expression returns true.
(ii) If both or all the expression returns false.
a b Result (XOR)
0 0 0
0 1 1
1 0 1
1 1 0
0 – false
1 - true
13. 8/30/201613
XOR Operator in Programming
string name, password;
name = "Steven" ;
password = "Steven123" ;
//it returns false because both expression match.
if ((name == "Steven" ^ password == “Clark")
{
MessageBox.Show(“Access granted…");
}
else
{
MessageBox.Show (“Access Denied. Aborting…”);
}