Tuesday, July 9, 2013

Logical Operators in C

Logical Operators are used to perform some logical operations in C. Sometimes, we need to use more than one relational operator in C. In such cases we use this kind of logical operators to connect the relational operators.

There are three logical operators. The following table gives a description of the logical operators.


Operator
Symbol
Example
Description
AND
&&
Exp1&&Exp2
To check if both the expressions are carried or not
OR
||
Exp1||Exp2
To check if either of the expressions are carried.
NOT
!
!Exp1
To check if Exp1 is not valid

Consider the following examples, to understand the logical operators clearly.
  • (x==2)&&(y==3)
It means that x should be 2 and also y should be 3, then only the whole equation returns 1.

  •  (x==2)||(y==3)
It means that either x is 2 or y is 3, then it is true, otherwise false.
  • !(x==2)
It means that if x is not 2, then it is true.

In the later sections, we will be seeing more of these operators.