Logical operators in C

Logical operator in C

Logical operator are mainly used to control program flow.Logical operator are used to compared between two or more conditions either true or false in Boolean value 1 or 0.

There are three types of logical operator.

  • AND OPERATOR
  • OR    OPERATOR
  • NOR OPERATOR
Logical operator

Implementation of Logical Operator

AND OPERATOR

In this operator compared two expression, expression 1 and expression 2.   If the both expression is true then result is true, Else any expression is false then result is false.

The symbol of AND operator is  &&.

1 and 1 = 1 = true.

1 and 0 = 0 = false

0 and 1 = 0 = false

0 and 0 = 0 = false.

OR OPERATOR

OR operator is a Boolean operator  that returns a value of TRUE if either (or both) of its operands is TRUE. This is called an inclusive OR Operator. If any expression is true then result is also true else both expression is false then result false.

The symbol of AND operator is ||.

1 and 1 = 1 = true.

1 and 0 = 0 = true

0 and 1 = 0 = true

0 and 0 = 0 = true.

NOR OPERATOR

The NOR Operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.

The symbol of AND operator is !.

Code for Logical operator in C

#include <stdio.h>
#include<stdlib.h>


int main()
{
int a,b,c,d;
a=20;
b=(a>10&&a<60);
c=(a<10||a<60);
d=!(a>10);

printf("%d",b);
printf("\n%d",c);
printf("\n%d",d);

return 0;
}
Output
1

1

0