Sunteți pe pagina 1din 14

Logical Operators

S.Sakthybaalan

ICT Level V
COT - Jaffna
1
2 8/30/2016

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
3 8/30/2016

Operators
Arithmetic Operators

Misc Operators Relational Operators

Assignment Operators
Logical Operators
Bitwise Operators
4 8/30/2016

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.

Logical Operators

AND XOR

OR NOT
5 8/30/2016

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:

operands

5+x
operator
 Note:
 All expressions have at least one operand.
6 8/30/2016

AND Operator (&&)


 The AND operator is used to compare two Boolean values.

a b Result (AND)
0 0 0
0 – false
0 1 0
1 0 0 1 - true
1 1 1

 It returns true if both of the operands are true.


7 8/30/2016

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/2016

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 – false
0 0 0

}
0 1 1
1 0 1
1 - true
1 1 1
9 8/30/2016

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/2016

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.

0 – false
a Result (NOT)
0 1 1 - true
1 0
11 8/30/2016

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/2016

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 – false


0 0 0
0 1 1
1 - true
1 0 1
1 1 0
13 8/30/2016

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…”);
}
14 8/30/2016

S-ar putea să vă placă și