Logical Operators

Logical Operators

C language allows the use of three logical operators;

  • AND &&
  • OR ||
  • Not !

It is important that while using logical operators, one must use these double symbols while using AND and OR operators, && and || respectively. The single symbols has some other application, they are called bitwise operators, like bitwise AND & and bitwise OR |

AND and OR Operators:

The AND and OR operators allow two or more conditions to be combined in an if statement.

Evaluation Chart:

Input Output
AND 1 1 1
1 0 0
0 1 0
0 0 0
OR 1 1 1
1 0 1
0 1 1
0 0 0

It is obvious from the table that;

  • AND operator produced a “true” result only when both of the inputs (expressions) are true, if any one of the expressions is false then the AND operator yields a “false” result
  • OR operator produces “true” result when any of the expressions is true, it only produces “false” result when both of the expressions are false.

The ! NOT Operator:

It is the third logical operator that can be used to define conditions in decision control structures. It reverses the value of the expression it is applied to.

For Example:

If there is a variable named “y” and its value is 10.

Then if we write this expression “!(y>5)”

This express will be evaluated as “False” or “0”

Because [y>5 = true] and [!true = false]

Evaluation Chart:

Input Output
NOT 1 0
0 1



Why to use Logical Operators:

The condition that we write using the logical operators can also be coded using nested if-else structures. But there are some drawbacks of this approach;

  • Indentation is required in order to use nested if-else clause, this drags the program to right side, making it difficult to read.
  • With each block of if-else curly brackets are needed, it is possible to skip any one, that will cause bugs difficult to find.
  • It is difficult to keep track of multiple if-else block nested into each other.

To avoid all these problems, we use logical operators;

  • Using logical operators reduce lines of code.
  • It easy to track them
  • These are an easy and efficient way to make conditions for filtering data.